How To: Change the row Background color based on the database value using GridView Control

We have seen many examples on how to change the background color of row based on the data using DataGrid Control. This can be done using a helper function or using the appropriate event of the datagrid

We will use the similar scenerio to find out how to use GridView Control in such case.

Step 1: Drag Drop GridView Control

<asp:GridView ID="GridView1" runat="server" DataSourceID="SqlDataSource1"  ></asp:GridView>

Step 2: We will use SqlDataSource control for the connectivity with the Sql Database

Lets use web.config for Sql Connection string:

<connectionStrings>

connectionStrings>

<add name="DummyDB" connectionString="Server=localhost;Integrated Security=True;Database=DummyDB;Persist Security Info=True" providerName="System.Data.SqlClient" />

</connectionStrings >

</connectionStrings >

The connection string is used with the Sql Datasource control as follows in the respective page

<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:DummyDB %>" SelectCommand="Select * from products"></asp:SqlDataSource>

Step 3:  We have Products table with field Unitprice as one of the fields. We will change the row background color to red if the UnitPrice<10

Step 3:  We have Products table with field Unitprice as one of the fields. We will change the row background color to red if the UnitPrice<10

This is done using the RowDataBound event

<asp:GridView ID="GridView1" runat="server" DataSourceID="SqlDataSource1" OnRowDataBound="GridView1_RowDataBound" >

</asp:GridView>

Step 4: We will code as follows for the RowDataBound Event.

VB.NET

Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs)

Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs)

   If e.Row.RowType = DataControlRowType.DataRow Then

      If (e.Row.DataItem("UnitsInStock") < 20) Then

         e.Row.BackColor = Drawing.Color.Red

      End If

   End If

End Sub

Thats All!

 

29 Comments

  • Many thanks for that. Simple bit of code, but could i work it out .. ? No. Cheers Works a TREAT :-)

  • Hi there , I can't Convert this code to c# , help me please , waiting for reply

  • c# code :

    if ( ((int)DataBinder.Eval(e.Row.DataItem, "UnitsInStock")) > 20 ) ....

  • Code is not working , it's is not finding the Control in the GridView

  • If you want to use a custom color such as the HTML one you've specified you need to use the ColorTranslator functionality.

    e.Row.BackColor = System.Drawing.ColorTranslator.FromHtml("#FF3399");

  • Nice. I have got it.

  • Thanx alot.It realy works fine and was a great help.

  • it is exactly what I was looking for..

    Thanks a lot people...

    Sabbiamobile, thanks for C# code..

    google and blogs like this make life much easier.. ;)

  • i hv a gridview ,in every row of gridview i have linkbuttons, i want to change the color of that particular row in which linkbutton is clicked

  • Thanx for code but I want C# code


  • Protected Sub gvEmployeeActivities_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gvEmployeeActivities.RowDataBound
    If e.Row.RowType = DataControlRowType.DataRow Then
    Dim lblActivityStatus As Label = e.Row.FindControl("lblActivityStatus")
    If Not lblActivityStatus Is Nothing Then


    If lblActivityStatus.Text.Trim() = "Started" Then
    e.Row.ForeColor = Drawing.Color.Green
    ElseIf lblActivityStatus.Text.Trim() = "Stopped" Then
    e.Row.ForeColor = Drawing.Color.Red
    ElseIf lblActivityStatus.Text.Trim() = "Completed" Then
    e.Row.ForeColor = Drawing.Color.Blue
    End If

    ' e.Row.ForeColor = Drawing.Color.Red
    End If
    End If
    End Sub

  • i need to change the text color depending on the text value of a perticular collunm and not the back color of the grid view. any help...


  • i neeed to just change the color of just the first row that is coming in the grid and leave all the rest of the rows unchanged.
    plssss help me it realy urgent

  • i need c# code plz help me..

  • protected void GridView1_OnRowDataBound(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
    {
    if (e.Row.RowType == DataControlRowType.DataRow) {
    if ((string)(DataBinder.Eval(e.Row.DataItem,"RFIDTag")) == "" ){
    e.Row.BackColor = System.Drawing.Color.LightBlue;
    }
    }
    }

  • like this its not working for which is applied with css

  • Thanx !! works like a charm amp0201 !!!

  • thanks, worked perfectly

  • thanks a lot...... :)

  • It was useful for me...

  • Thanks for the code. The code does not work in the following scenario though. A button on gridview opens an AjaxModalPopup. If I press cancel on the popup the colour goes away. any idea?

  • The colour for rows does not work even when a button/ link is pressed.

  • protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
    e.Row.Attributes.Add("style", "cursor:help;");
    if (e.Row.RowType == DataControlRowType.DataRow && e.Row.RowState == DataControlRowState.Alternate)
    {
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
    e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='orange'");
    e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='#E56E94'");
    e.Row.BackColor = Color.FromName("#E56E94");
    }
    }
    else
    {
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
    e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='orange'");
    e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='gray'");
    e.Row.BackColor = Color.FromName("gray");
    }
    //e.Row.Cells[0].BackColor = Color.FromName("gray");
    //e.Row.Cells[1].BackColor = Color.FromName("gray");
    //e.Row.Cells[2].BackColor = Color.FromName("gray");
    //e.Row.Cells[3].BackColor = Color.FromName("gray");
    //e.Row.Cells[4].BackColor = Color.FromName("gray");
    //e.Row.BorderWidth = 2;
    //e.Row.BorderColor = Color.FromName("#43C6DB");
    }
    }

  • How To_3A00_ Change the row Background color based on the database value using GridView Control.. WTF? :)

  • Can you do it in c# so that we can able to use it in project

  • I really liked the article, and the very cool blog

  • hi

    i already have a color and stored it in arraylist.

    i have to plot the color for N number of rows.

    GridView1.rows[i].backcolor=(color)ar[i];
    here ar contains the color..

    but it is plotting for the last row only..

    ie,it is plotting for last row with the last color..

    what to do...

  • Hi,

    In order to change one single row color amongst numerous rows in a gridview, in asp.net, the following code can be used.

    DataTable dt = new Datatable(); --> assign this table with some values from database.

    for (int k = 0; k < dt.Rows.Count; k++)
    {
    for (int i = 0; i < dt.Rows[k].Cells.Count;
    i++)
    {
    dt.Rows[k].Cells[i].ForeColor = Color.Red;

    }
    }

  • This website is great. I like it.(www.linkspirit.net)N_X_D_S.

Comments have been disabled for this content.