Highlighting Row of GridView in Specific Condition

Background

I have decided to write this article considering the requirements of Job Seekers that often in an interview the question is asked how to highlight a specific row of a GridView.

So let us see in this article practically as well as theoretically how to answer the interviewer when the following types of questions are asked:

Question : Can we a specific row and cell of GridView?

Ans: Yes, why not?

Question: Ok, how to do it?

Ans: To highlight a specific row or cell in GridView, I will write the code on GridView OnRowDataBound Event.

Question: Ok, why on the OnRowDataBound Event?

Ans: Because the OnRowDataBound Event is raised when the row is bound to the GridView from data source so this is event is suitable to highlight a row.

Question: That's good. Why do we need to highlight a row or cell of GridView?

Ans: Consider the example of when we need to quickly see the salaries greater than 5000 of employees and in many conditions we need this type of requirement.

So, these types of questions are often asked in an interview so let us see practically how to use the "OnRowDataBound" Event .

Create a table named student as:

Highlighting-Row-of-Gridview-In-Specific-Condition1.jpg

Insert the some records in the table as:

  1. insert into Student Values('Sudhir','Latur',400)  
  2. insert into Student Values('Kapil','Udgir',300)  
  3. insert into student values('Priya','Pune',500)  
  4. insert into student values('Vithal','Mumbai',600)
Note: To learn how to insert records into a database table programmatically in ASP.Net C#, please see my article Inserting Form Data Into DataBase Using Stored Procedure In ASP.NET C#.

To see the records of the table that we inserted use:
  1. select *from student
The output will be:

Highlighting-Row-of-Gridview-In-Specific-Condition2.jpg
Hope you have created the same type of table.

Now let us start to create a Web application, step-by-step.

Now create the project  as:

  1. "Start" - "All Programs" - "Microsoft Visual Studio 2010".
  2. "File" -> "New" -> "Project..." -> "C#" -> "Empty Project" (to avoid adding a master page).
  3. Give the project a name, such as "GridRowHighlighting" or another as you wish and specify the location.
  4. Then right-click on Solution Explorer and select  "Add New Item" then create a "Default.aspx" page. 
  5. Add one  GridView in the Default.aspx page.

Then the <form> section of the Default aspx page will look as in the following:

  1. <form id="form1" runat="server">  
  2. <asp:GridView ID="GridView1" runat="server" >  
  3.     </asp:GridView>   
  4.     </div>  
  5. </form>   

Now switch to design mode and use the following code.

To bind the grid, create the following method:

Highlighting-Row-of-Gridview-In-Specific-Condition3.jpg

And call the method above on page load as:

Highlighting-Row-of-Gridview-In-Specific-Condition4.jpg

Now create the method that will be called on the "onrowdatabound" event of the GridView as:

Highlighting-Row-of-Gridview-In-Specific-Condition5.jpg

In the method explained above I have written the code just for highlighting one row. Similarly you can write it for others or you can download the zip file above. Now call the method above on the GridView onrowdatabound event as:

 onrowdatabound="highlightrow"

Now run the application, the output will be as:

Highlighting-Row-of-Gridview-In-Specific-Condition6.jpg

In the preceding screen, as you see the specific rows are highlighted for the specific condition..

Note 

  • For detailed code please download the zip file attached above.

  • Don't forget to apply the relevant changes in the "Web.config" file depending on your server location. 

Summary
 
Now we have learned how to highlight the specific row of  GridView. I hope this article is useful for all students and beginners. If you have any suggestion related to this article please contact me.