Click here to Skip to main content
11,926,429 members (55,200 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


6 bookmarked

Changing the color of a row in a GridView in ASP.NET

, 27 Aug 2011 CPOL
Rate this:
Please Sign up or sign in to vote.
How to change the color of a row in a GridView in ASP.NET.

This article describes changing the color of a GridView row depending on the value of a column and changing the value of a column depending on the value of that column.

First, let's start with binding a GridView with data from the database. Create a GridView.

<asp:GridView ID="grdDoc" runat="server" AllowPaging="True" 
        AutoGenerateColumns="False" DataKeyNames="DOCCODE" Height="40px" 
        PageSize="8" ShowFooter="True"   Visible="false" 
        onselectedindexchanged="grdDoc_SelectedIndexChanged" >
    <PagerSettings Mode="NumericFirstLast" />
    <RowStyle BackColor="WhiteSmoke" BorderColor="CornflowerBlue" 
        ForeColor="Black" Height="30px" />
        <asp:TemplateField HeaderText="Dose">
                <asp:LinkButton ID="LinkButton1" runat="server" 
        <asp:BoundField DataField="DOCNAME" HeaderText="Description" />
        <asp:BoundField DataField="DOCDEPT" HeaderText="Description" />
        <asp:BoundField DataField="DOCACTIVE" HeaderText="Description" />
    <FooterStyle BackColor="Silver" Height="25px" />
    <PagerStyle BackColor="DarkGray" />
    <HeaderStyle BackColor="#000084" Font-Bold="True" ForeColor="White" />

Load data from a database:

protected void Page_Load(object sender, EventArgs e)
    string strSQLconnection = 
      "Data Source=dbServer;Initial Catalog=testDB;Integrated Security=True";
    SqlConnection sqlConnection = new SqlConnection(strSQLconnection);
    SqlCommand sqlCommand = new SqlCommand("select DOCCODE,DOCNAME," + 
        "DOCDEPT, DOCACTIVE from DOCTORS", sqlConnection);
    SqlDataReader reader = sqlCommand.ExecuteReader();
    grdDoc.DataSource = reader;

Change the color of a row depending on whether the Doctor is Active or Inactive.

protected void grdDoc _RowDataBound(object sender, GridViewRowEventArgs e)
        if (e.Row.RowType == DataControlRowType.DataRow)
            if ((string.IsNullOrEmpty(e.Row.Cells[3].Text) != true) || 
                       (e.Row.Cells[3].Text != "&nbsp;"))
                Char result = Convert.ToInt32(e.Row.Cells[3].Text);
                if (result == ‘Y’) e.Row.BackColor = System.Drawing.Color.Aqua;
                else if (result ==’N’) e.Row.BackColor = System.Drawing.Color.Cornsilk;

First, we need to check if the data in the fourth column is null or the column is empty. If the Doctor is Active, then the row color will be AQUA. Or else the Color of the row will be CORNSILK.

Change the Color of the cells depending on the Value of a column:

protected void grdDoc _RowDataBound(object sender, GridViewRowEventArgs e)
        if (e.Row.RowType == DataControlRowType.DataRow)
            if ((string.IsNullOrEmpty(e.Row.Cells[3].Text) != true) 
                  || (e.Row.Cells[3].Text != "&nbsp;"))
                string result = Convert.ToInt32(e.Row.Cells[2].Text);
                if (result == “M.S”)
                    e.Row.Cells[3].BackColor= System.Drawing.Color. Aquamarine;
                    e.Row.Cells[3].BackColor= System.Drawing.Color. BlanchedAlmond;


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

raj ch
India India
No Biography provided

You may also be interested in...

Comments and Discussions

GeneralMy vote of 4 Pin
Upasak Poddar8-Jul-13 23:58
memberUpasak Poddar8-Jul-13 23:58 
Questionchange colours in rows of gridview Pin
Member 96818625-Feb-13 0:45
memberMember 96818625-Feb-13 0:45 
QuestionNice Pin
JrPacheco7-Nov-12 4:08
memberJrPacheco7-Nov-12 4:08 
GeneralReason for my vote of 4 good code !! Pin
Sunnykumar0831-Oct-11 22:37
memberSunnykumar0831-Oct-11 22:37 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.151126.1 | Last Updated 27 Aug 2011
Article Copyright 2011 by raj ch
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid