Search code examples
c#asp.netvisual-studiovisual-studio-2005

Highlight GridView row when a condition is met


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?


Solution

  • 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;
            }
        }
    }