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......

Dani AI

Generated

As and pointed out, the right place to apply row-level styling is while rows are being created — the GridView’s Rows collection is empty until after DataBind, which explains the zero count in the original loop. A robust pattern is to handle RowDataBound and read the underlying data field (not a hard-coded cell index), then set a BackColor or a CSS class. Using the data item avoids problems when columns are autogenerated or reordered.

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType != DataControlRowType.DataRow) return;

    string status;
    if (e.Row.DataItem is DataRowView drv)
        status = Convert.ToString(drv["result"]);
    else
        status = Convert.ToString(DataBinder.Eval(e.Row.DataItem, "result"));

    status = status?.Trim() ?? string.Empty;

    if (string.Equals(status, "NO", StringComparison.OrdinalIgnoreCase))
        e.Row.BackColor = System.Drawing.Color.LightCoral;
    else if (string.Equals(status, "YES", StringComparison.OrdinalIgnoreCase))
        e.Row.BackColor = System.Drawing.Color.LightBlue;
}

For maintainability prefer CSS classes instead of inline colors:

.status-no { background:#f2dede; color:#a94442; }
.status-yes { background:#d9edf7; color:#31708f; }

Then set e.Row.CssClass = "status-no" or "status-yes" in RowDataBound. If using a TemplateField with a Label, find the control (e.Row.FindControl("lblResult")) and read its Text. Additional notes: bind the grid (DataSource + DataBind) before inspecting Rows; use if (!IsPostBack) in Page_Load unless dynamic refresh is needed; RowDataBound runs per visible page when paging is on, so style the data source if all rows must be marked. Finally, watch for the common typo of using = instead of == or string.Equals when comparing strings.

Recommended Answers

All 3 Replies

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?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.