RowDataBound Event In GridView In ASP.NET

This example shows how to use a RowDataBound event in a GridView to highlight the rows or cells in ASP.NET. This example is helpful in situations where you need to highlight the GridView rows or cells based on a specific condition. For demonstration purposes, I created a database (named Database.mdf) in which we have a table (named tbl_Employee).
 
The following is the table schema used in this example:
  1. CREATE TABLE [dbo].[tbl_Employee] (  
  2.     [Id]     INT          NOT NULL,  
  3.     [Name]   VARCHAR (50) NULL,  
  4.     [City]   VARCHAR (50) NULL,  
  5.     [Salary] INT          NULL,  
  6.     PRIMARY KEY CLUSTERED ([Id] ASC)  
  7. );  
Let's begin. Use the following procedure.
  1. Drop a GridView control from the toolbox and set the AutoGenerateColumns property to false.
  2. Add a Columns Collection (element) to manage the collection of Column fields.
  3. Inside the Columns tag, add a column field (BoundField) that displays the value of a field in a data source.
  4. Set the DataField property to the name of the column in the table for binding to the BoundField object and set the HeaderText value for displaying it on the GridView's Header.
  5. Add a RowDataBound Event to the GridView.
Default.aspx Code 
  1. <form id="form1" runat="server">  
  2.     <div>  
  3.         <asp:GridView ID="GridView1" runat="server" CellPadding="6" AutoGenerateColumns="False" OnRowDataBound="GridView1_RowDataBound">  
  4.             <Columns>  
  5.                 <asp:BoundField DataField="Id" HeaderText="Id"/>  
  6.                 <asp:BoundField DataField="Name" HeaderText="Name"/>  
  7.                 <asp:BoundField DataField="City" HeaderText="City"/>  
  8.                 <asp:BoundField DataField="Salary" HeaderText="Salary"/>  
  9.             </Columns>  
  10.         </asp:GridView>  
  11.     </div>  
  12. </form>  
In this example, I will highlight the complete row for the Employee with a Salary less than 10000.

Default.aspx.cs Code
  1. using System;  
  2. using System.Web.UI.WebControls;  
  3. using System.Data;  
  4. using System.Data.SqlClient;  
  5. using System.Configuration;  
  6. using System.Drawing;  
  7.   
  8. public partial class _Default : System.Web.UI.Page  
  9. {  
  10.     SqlConnection con;  
  11.     //Connection String from Web.config file  
  12.     string cs = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;  
  13.     protected void Page_Load(object sender, EventArgs e)  
  14.     {  
  15.         if(!IsPostBack)  
  16.         {  
  17.             ShowData();  
  18.         }  
  19.     }  
  20.     //method for Displaying Data in Gridview  
  21.     protected void ShowData()  
  22.     {  
  23.         DataTable dt = new DataTable();  
  24.         con = new SqlConnection(cs);  
  25.         SqlDataAdapter adapt = new SqlDataAdapter("select * from tbl_Employee",con);  
  26.         con.Open();  
  27.         adapt.Fill(dt);  
  28.         con.Close();  
  29.         GridView1.DataSource = dt;  
  30.         GridView1.DataBind();  
  31.     }  
  32.     //RowDataBound Event  
  33.     protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)  
  34.     {  
  35.         //Checking the RowType of the Row  
  36.         if(e.Row.RowType==DataControlRowType.DataRow)  
  37.         {  
  38.             //If Salary is less than 10000 than set the row Background Color to Cyan  
  39.             if(Convert.ToInt32(e.Row.Cells[3].Text)<10000)  
  40.             {  
  41.                 e.Row.BackColor = Color.Cyan;  
  42.             }  
  43.         }  
  44.     }  
  45. }  
Preview
 
 
 
Example 2

Suppose we need to highlight the name of Employee but not the entire row of the GridView. We can do that using the following code:
  1. protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)  
  2. {  
  3.         //Checking the RowType of the Row  
  4.         if (e.Row.RowType == DataControlRowType.DataRow)  
  5.         {  
  6.             //If Salary is less than 10000 than set the Cell BackColor to Red and ForeColor to White  
  7.             if (Convert.ToInt32(e.Row.Cells[3].Text) < 10000)  
  8.             {  
  9.                 e.Row.Cells[1].BackColor = Color.Red;  
  10.                 e.Row.Cells[1].ForeColor = Color.White;  
  11.             }  
  12.         }  
  13. }      
Preview
 
 

I hope you like it. Thanks.

My other articles on GridView: 


Similar Articles