Highlight GridView row when a condition is met

20,528

Solution 1

Yes, add OnRowDataBound="yourGridview_RowDataBound" to your gridview. This event gets triggered for every gridview row.

In the code behind, have this:

public void yourGridview_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        // do your stuffs here, for example if column risk is your third column:
        if (e.Row.Cells[2].Text == "high")
        {
            e.Row.BackColor = Color.Red;
        }
    }
}

Solution 2

Use RowDataBound Event. In this event you would get to add the css based upon your condition

 void GridView_RowDataBound(Object sender, GridViewRowEventArgs e)
  {

    if(e.Row.RowType == DataControlRowType.DataRow)
    {
      // Logic for High
      if(e.Row.Cells[1].Text > 100)
      //set color
      e.Row.Attributes.Add("style", "this.style.backgroundColor = '#FFFFFF';");

    }

  }

Solution 3

in RowDataBound try:

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    // searching through the rows
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
      if(int.Parse(DataBinder.Eval(e.Row.DataItem,"Risk").ToString()) > 100)
        {
            e.Row.BackColor = Color.FromName("#FAF7DA"); // is a "new" row
        }
    }
}

Solution 4

You should subscribe for the RowDataBound event of the grid and catch hold of the row which has your column mentioning Risk as High then set the BackColor of the row to your highlighting color choice

 If (e.Row.RowType == DataControlRowType.DataRow)
 {
        //DataBinder.Eval(e.Row.DataItem,"Risk"))
        //if this is high then set the color
        e.Row.BackColor = Drawing.Color.Yellow
 }

MSDN Formatting the GridView Based on the Underlying Data

Share:
20,528
gymcode
Author by

gymcode

Updated on March 13, 2020

Comments

  • gymcode
    gymcode about 4 years

    I'm using VS2005 C# Server-side coding.

    I'm curious to know that in VS2005 version, is it possible to highlight a row in a GridView when a condition is met? E.g. If column Risk is stored as high in the database for that specific row, the row will be highlighted in Red.

    Is it possible?


    Edit:

    Current code:

    protected void GridView1_OnRowDataBound(Object sender, GridViewRowEventArgs e)
    {
    
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        // do your stuffs here, for example if column risk is your third column:
        if (e.Row.Cells[3].Text == "H")
        {
            e.Row.BackColor = Color.Red;
        }
    }
    }
    

    I assume column cell starts from 0, so mine is at cell 3. But the color still does not change.

    Anyone has any idea?

  • Pupper
    Pupper over 12 years
    Check your e.Row.Cells[3].Text is really equal to "High" (correct cases, no extra spaces, etc). You can use Response.Write("-" + e.Row.Cells[3].Text + "-") to display the values.
  • gymcode
    gymcode over 12 years
    It is correct, at the top of my page is full of the text in my third column.
  • Pupper
    Pupper over 12 years
    Try temporarily disable "if (e.Row.Cells[3].Text == "H") { }" and just leave out "e.Row.BackColor = Color.Red;". Does the color change now?
  • gymcode
    gymcode over 12 years
    Yes it does, the whole table turns red.
  • Pupper
    Pupper over 12 years
    Then it means "if (e.Row.Cells[3].Text == "H")" never returns true. I can't see your data so cannot tell why they don't match up but you should be able to identify any difference between the two.
  • Eduard Malakhov
    Eduard Malakhov over 6 years
    It would be helpful if you added a description of your idea, not plain code.