Handle the rowdatabound event. Check e.Row.RowType to make sure you're dealing with the right type of row. Then use e.Row.FindControl to find/read the value and based on that you decide whether to change e.Row.BackColor.
The column in question is column numbered 6 (stating the first count at 0)... It will either be blank or populated with a data value... those with a date value... i want the colour to change.... I've added the following code to my vb page... but its not
doing anything?
Public Class PatientList
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
End Sub
Shared Function aspx() As String
Throw New NotImplementedException
End Function
Private Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
If True Then
If True Then
If e.Row.RowType = DataControlRowType.DataRow Then
If (String.IsNullOrEmpty(e.Row.Cells(6).Text) <> True) OrElse (e.Row.Cells(6).Text <> " ") Then
Dim result As [Char] = (e.Row.Cells(6).Text)
If result = "Y" Then
e.Row.BackColor = System.Drawing.Color.Aqua
ElseIf result = "N" Then
e.Row.BackColor = System.Drawing.Color.Cornsilk
End If
End If
End If
End If
End If
End Sub
End Class
So, you are saying that column 6 has 'Y...' or 'N...' and the color is not changing? Could there be a space before the first character? If so, trim the text. If the text is 'y...' or 'n...' make it upper case, .
Dim result As [Char] = (e.Row.Cells(6).Text.ToUpper.Trim)
PS - Why do you have the 2 'If True Then' lines of code?
sam233
Member
36 Points
218 Posts
Changing the color of a row in a GridView
Dec 28, 2012 09:49 AM|LINK
How do I change the color of a GridView row depending on the value of a column ?
Thanks....
</div>MetalAsp.Net
All-Star
112168 Points
18255 Posts
Moderator
Re: Changing the color of a row in a GridView
Dec 28, 2012 09:59 AM|LINK
Handle the rowdatabound event. Check e.Row.RowType to make sure you're dealing with the right type of row. Then use e.Row.FindControl to find/read the value and based on that you decide whether to change e.Row.BackColor.
SohailShaikh
Contributor
6129 Points
1172 Posts
Re: Changing the color of a row in a GridView
Dec 28, 2012 09:59 AM|LINK
check this link for more n more learning
http://asp-net-example.blogspot.com/2008/11/aspnet-gridview-rowdatabound-event.html
Sohail Shaikh
kirupa.v
Contributor
2070 Points
531 Posts
Re: Changing the color of a row in a GridView
Dec 28, 2012 10:06 AM|LINK
Hi,
Check for the following links
http://www.highoncoding.com/Articles/174_Change_GridView_RowColor_OnMouseClick.aspx
http://www.highoncoding.com/Articles/172_Changing_GridView_Row_Color_OnMouseOver.aspx
Reply me for any issues..
sam233
Member
36 Points
218 Posts
Re: Changing the color of a row in a GridView
Dec 28, 2012 10:32 AM|LINK
thanks for the links guy, very helpful...
The column in question is column numbered 6 (stating the first count at 0)... It will either be blank or populated with a data value... those with a date value... i want the colour to change.... I've added the following code to my vb page... but its not doing anything?
Public Class PatientList Inherits System.Web.UI.Page Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load End Sub Shared Function aspx() As String Throw New NotImplementedException End Function Private Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound If True Then If True Then If e.Row.RowType = DataControlRowType.DataRow Then If (String.IsNullOrEmpty(e.Row.Cells(6).Text) <> True) OrElse (e.Row.Cells(6).Text <> " ") Then Dim result As [Char] = (e.Row.Cells(6).Text) If result = "Y" Then e.Row.BackColor = System.Drawing.Color.Aqua ElseIf result = "N" Then e.Row.BackColor = System.Drawing.Color.Cornsilk End If End If End If End If End If End Sub End Classpaindaasp
Star
12090 Points
2034 Posts
Re: Changing the color of a row in a GridView
Dec 28, 2012 12:56 PM|LINK
Is column 6 a bound or TemplateField?
sam233
Member
36 Points
218 Posts
Re: Changing the color of a row in a GridView
Dec 28, 2012 01:13 PM|LINK
<asp:BoundField DataField="datedeath" HeaderText="Date of death" SortExpression="datedeath" />paindaasp
Star
12090 Points
2034 Posts
Re: Changing the color of a row in a GridView
Dec 28, 2012 01:24 PM|LINK
So, you are saying that column 6 has 'Y...' or 'N...' and the color is not changing? Could there be a space before the first character? If so, trim the text. If the text is 'y...' or 'n...' make it upper case, .
PS - Why do you have the 2 'If True Then' lines of code?
sam233
Member
36 Points
218 Posts
Re: Changing the color of a row in a GridView
Dec 28, 2012 01:43 PM|LINK
tried the solution u provided but no luck matke...
It does nothing at all...
paindaasp
Star
12090 Points
2034 Posts
Re: Changing the color of a row in a GridView
Dec 28, 2012 01:53 PM|LINK
Have you run in debug and set breakpoints to see exactly what is in e.Row.Cells(6).Text?