I have a GridView
tied to a table in a SQL Database so that it loads onto a page after login to my website.
When the page with the GridView
is first loaded. I step through the rows on the table and change the BackColor
for each row depending on it's "Status" column (i.e. if it's "Incomplete" make it red or green if it's "Complete"). When you sort the table by another column then the BackColor
goes away.
I attempt to run the same function to step through the rows and change all the BackColors
on the GridView1_Sorted
event but the table remains without any color changes. Same goes for using the GridView1_Load
event. However adding a button in and tieing that button's click to the same formatting code lets me apply the BackColors
after sorting.
This tells me I'm probably misunderstanding something in how the Sorted
event works. Can anyone tell me how to get the GridView
set up properly so that I can sort by some column and still re-apply my BackColor
formatting?
Try something like this
If e.Row.RowType = DataControlRowType.DataRow Then
Dim stStatus As String = e.Row.Cells(1).Text ' INDEX OF YOUR STATUS COLUMN
For Each cell As TableCell In e.Row.Cells
If stStatus = "Incomplete" Then
cell.BackColor = Color.Red
Else If stStatus = "Complete" Then
cell.BackColor = Color.Green
End If
Next
End If
Put this code in the RowDataBound
event of your GridView