showing something different from linq query result in gridview

Last post 06-26-2008 11:26 PM by Qin Dian Tang - MSFT. 4 replies.

Sort Posts:

  • showing something different from linq query result in gridview

    06-24-2008, 1:15 PM
    • Loading...
    • ashkant
    • Joined on 06-10-2008, 7:32 AM
    • Posts 157

    hi everyone

    i use linq to databind a gridview

    when i select 0 from database i want to show it as string "lose" in gridview

    and when selecting 1 i want to show it as string " win" in gridview

    i use to write code using selecting event like below  

    protected void LinqDataSource1_Selecting(object sender, LinqDataSourceSelectEventArgs e)

    {

    some query

    }

    i dont know where to imply "if" in my code or maybe i shouldn't use "if " here in CS and should do sth when showing data in my aspx page.

    any suggestion?

    Please remember to click “Mark as Answer” on the post that helps you
  • Re: showing something different from linq query result in gridview

    06-24-2008, 1:27 PM
    Answer
    • Loading...
    • ecbruck
    • Joined on 12-30-2005, 2:39 PM
    • Des Moines, IA
    • Posts 7,995
    • Moderator
      TrustedFriends-MVPs

    Why not simply modify your data-binding expression to do the conversion for you?

    <%# Eval("MyField").ToString().Equals("0") ? "lose" : "win" %>

    Thanks, Ed

    Microsoft MVP - ASP/ASP.NET

  • Re: showing something different from linq query result in gridview

    06-26-2008, 3:31 AM
    Answer
    • Loading...
    • ashkant
    • Joined on 06-10-2008, 7:32 AM
    • Posts 157

    you are right. i dont know why i didn't use this!!!

    can i use something like this to set colors too? (without connecting back to database). for example label.text="win"? "Green" : "Red" .

    i had problem writing that.help plz

    thank you

    Please remember to click “Mark as Answer” on the post that helps you
  • Re: showing something different from linq query result in gridview

    06-26-2008, 8:02 AM
    • Loading...
    • ecbruck
    • Joined on 12-30-2005, 2:39 PM
    • Des Moines, IA
    • Posts 7,995
    • Moderator
      TrustedFriends-MVPs

    For that, you'd simply have to grab a reference to the current row within the GridView.RowDataBound event. Then, depending upon your value, you can change anything you want with regards to the current row or any cell or control within.

    Thanks, Ed

    Microsoft MVP - ASP/ASP.NET

  • Re: showing something different from linq query result in gridview

    06-26-2008, 11:26 PM
    Answer

    Hi ashkant,

    Try this:

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                Label l = (Label)e.Row.FindControl("Label1");
                e.Row.BackColor = (l.Text == "win") ? System.Drawing.Color.Green : System.Drawing.Color.Red;
            }
        }

    Thanks,

    Please remember to click "Mark as Answer" on the post that helps you, and to click "Unmark as Answer" if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
Page 1 of 1 (5 items)
Microsoft Communities
Page view counter