Change Row Color of a Grid view Based on Particular Condition C#.Net ASP.Net VB.Net

In this article i am going to explain about how to change the row color of a particular row in grid view based on the particular condition to differentiate the rows. For explanation purpose i have created a table called Employee in my local database and inserted 10 sample records. And also i have created a procedure called GetEmployeeDetails which will return all the details of the employee tables. below is the sql script used.

Now create a new asp.net application and drog and drop the grid view control on your form. And set AutoGenerateColumns property to true(By default it is true. You no need to do it.) That is it. Below is the html markup of the code.

Now before proceeding further to write the cs code add connection string in web.config file

And now include the below namespaces in your code file.
C#.Net:

VB.Net:

And in page load write the code to fetch the data from your table and bind it to grid view. The code is given below.
C#.Net:

VB.Net:

Now if you run the code you will get the output as given below…

How To Change Row Color of a Particular Row in grid view Based on Particular Condition

But in my case i would like to differentiate the row based on the employee salary. For that one i added the OnRowDataBound=”grdProduct_RowDataBound” event of the grid view. The event handler code is given below.
C#.Net:

VB.Net:

Now if you will get the below output.

How To Change Row Color of a Particular Row in grid view Based on Particular Condition

 Download Source:

Comments

Subscribe For Latest Updates

Signup for our newsletter and get notified when we publish new articles for free!