Hello All,

I have a problem regarding the grid view win C#..

i have a grid view called subGrid, i am using it to get the values sent by a Stored Procedure..

subGrid.DataSource = form.GetRegistrant();

Now this subGrid has the following values..

id name result
1 a Yes
2 b Yes
3 c Yes
4 d NO
5 e NO

and i am using subGrid.DataBind();

to show it in my web page ....every thing is fine till here...

Now, i want to enhance it and show the values in the result (NO) as red back ground color and the values in result with an YES with a blue for ground color...can any1 help me in this plz......

You have to add the OnRowDataBound method for the gridview. so add this method and give it a name like this OnRowDataBound="BoundRow" then lets implement the method. in your source code create a method called BoundRow.

protected void BoundRow(object sender, GridViewRowEventArgs e)
{
      if (e.Row.RowType == DataControlRowType.DataRow)
      {
            if(e.Row.Cells[2].Text = "Yes")
            e.Row.Cells[2].ForeColor = System.Drawing.Color.SomeColor;     
            else
          e.Row.Cells[2].ForeColor = System.Drawing.Color.Someothercolor;     
      }
}

HI... I tried this...

for (i = 0; i <= submissionsGrid.Rows.Count - 1; i++)
                {
                    for (j = 0; j <= submissionsGrid.Rows[i].Cells.Count - 1; j++)
                    {
                        c = submissionsGrid.Rows[i].Cells[j].Text;
                        if (c == "NO")
                        {
                            submissionsGrid.Rows[i].ForeColor = System.Drawing.Color.Red;
                        }
                        
                    }
                    int k;
                    k = submissionsGrid.Columns.Count;
                }
                submissionsGrid.DataBind();

In the line subGrid.DataSource = form.GetRegistrant();

if i keep a break point and point to data source i could see the entire table with 113 rows...

but submissionsGrid.Rows.Count is showing zero....i dont know y....

OK, couple of things...the method given above is a good way to do it...you want to colour the rows when they are bound so use the RowDataBound event to do it. It saves you iterating through all your rows.

Secondly, you are checking each row of the datagrid's before you bind it. If you really want to do it your way, do the databind and then check the contents of each row. Also, you only need to check the result column so why are you iterating through every cell in the row?

This article has been dead for over six months. Start a new discussion instead.