ASP.NET Core 2.0 Hosting: RowDataBound Event In GridView In ASP.NET

In this tutorial, I will show you 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. hflnet51For 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:

Let’s begin. Use the following procedure.

  • Drop a GridView control from the toolbox and set the AutoGenerateColumns property to false.
  • Add a Columns Collection (element) to manage the collection of Column fields.
    Inside the Columns tag, add a column field (BoundField) that displays the value of a field in a data source.
  • 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.
  • Add a RowDataBound Event to the GridView.

Default.aspx Code

In this example, I will highlight the complete row for the Employee with a Salary less than 10000.
Default.aspx.cs Code

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: