We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now


Format rows in GridView on ASPX page

Medium Priority
Last Modified: 2013-11-26
Hello Experts,

I have an issues here, that sounds very complicated for a novice user like me :-(.

I am developing a website for my company to automate Windows Server Administration. Using this web based tool one can query windows server details like Hardware/Software/Ping Status/Network Configuration etc. To get these details I have an ASPX page with a GridView control placed on it. There are few text boxes for getting input from the user and few buttons to perform actions.

Coming to the issue:
The GridView control placed on the aspx page is used as common way to display output from any of the several buttons on the page. I am collecting the couput details in a DataTable during the code execution and finally binding the DataTable to the GridView to display the results.
There are different functions attached with each button click event. Every function is using different structure of columns to display output based on the purpose. For example: function to display ping status will use columns - ServerName, IPAddress, FQDN, PingStatus; whereas the function to display OS details will have columns such as - ServerName, OperatingSystem, ServicePack, SerialNumber, LastBootUp. Thus the same GridView will have different columns at different times.

Based on the value criterias defined in different functions I need to highlight the rows in GridView in different color (yellow background). For example: In the function to display windows Service details I need to highlight rows where Service status is Stopped. Whereas in the function to display Disk Usage I need to highlight the rows where disk free space percentage column is having values below 10%.

I have searched the web and found below (event handler) function to highlight the rows based on the cell value but it is depending on the RowDataBound Event.
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)  
        if (e.Row.RowType == DataControlRowType.DataRow)  
            if (e.Row.Cells[4].Text=="1")  
                e.Row.BackColor = Color.Green;  
                e.Row.BackColor = Color.Red;  

Open in new window

There are two issues with the above approach:
(1)  Since we are adding data to GridView by setting DataSource = DataTable and then binding the datatable, I am not sure if that will trigger the above RowDataBound event.
(2) And, since the column structure is not fixed due to different functions using same GridView with different column structure each time, in order to get the Column reference we need to first get reference of the function which is creating the GridView.

Hope this is clear. If not, please feel free to ask for more explaination or any other input from my side.
Watch Question

1. RowDataBound will be triggered when because you will make a call to GridView1.DataBind()

2. since the columns will be dynamic, You need to setup the colors before hand. You need to add the logic to determine the columns that will be colored.
 as an example, you can create two arrays = type{"ping","os"} column{"3","5"} color{"blue","red"}
in the example above, the Column 3, for the ping table will be colored Blue. whilst the 5th column of the 'os' table will be Red. (you will determine the values using the index.

Unlock this solution with a free trial preview.
(No credit card required)
Get Preview


I got some workaround that worked for me.
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a free trial preview!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.