SIGN UP MEMBER LOGIN:    
ARTICLE

Change Rows Background color in GridView with selected Criteria

Posted by Prabhakar Parihar Articles | ASP.NET Programming April 07, 2011
In this article we change background color of the rows in a GridView with selected criteria on GridView1_RowDataBound event.
Reader Level:


In this article we change background color of the rows in a GridView with selected criteria on GridView1_RowDataBound event.

GridCriteria1.gif

In aspx page

In an aspx page first add a GridView control then set the SqlDataSource for the GridView to the NorthWind database.

GridCriteria2.gif

In aspx.cs page

In aspx.cs page write the code for the Gridview1_RowDataBound Event. Set the criteria you want for the Gridview Row Backcolor Change. The GridView offers a RowDataBound event that fires once for each row after the row has been created and bound to the corresponding record of data from the data source control. In this article I am changing the row background color when CategoryID == 2; you can use whatever criteria you need to.

protected void Gridview1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            // determine the value of the UnitsInStock field
            int CategoryID = Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "CategoryID"));
            if (CategoryID == 2)
                // color the background of the row yellow
                e.Row.BackColor = System.Drawing.Color.Brown;
        }
 
    }


In aspx page

In the aspx page on the GridView set the RowDataBound event in the GridView property. Then, in the RowDataBound line, type in the name of the event handler you want to create (I named my event handler productsGridView_RowDataBound). Finally, enter in the following code into the event handler on OnRowDataBound="Gridview1_RowDataBound" .

GridCriteria3.gif

Conclusion

In this article we try to improve GridView design based on certain criteria. Hopefully it's useful for you.

Login to add your contents and source code to this article
share this article :
post comment
 

Just what I was looking for. Minor gripe it overrides the selected color but in some cases that's desirable.

Posted by Stephen Carroll May 25, 2011
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
Become a Sponsor