I want to change the background color of a cell based on data within the row. BUT...I get the following error: Databinding expressions are only supported on controls. System.Web.UI.WebControls.TemplateField is not a control.
When I attempt this:
<asp:TemplateField
ControlStyle-CssClass='<%# Eval("VAdmDtTm") %>'>
I have tried code-behind on all the row events, but I guess this code (which worked in ASP.NET 1.x ibut is no longer valid in 2.0): System.NullReferenceException was unhandled by user code
Message="Object reference not set to an instance of an object."
So - how can I set the cell background color based on data within the row?
several options here. You can use a template or you can handle the rowdatabound event. if you handle the event, you can simply change the background of the cell with code similar to this one:
void Handle( object sender, GriViewRowEventArgs args )
{
args.Row.Cells[put the position of the cell here].BackColor = your color here;
}
--
Regards,
Luis Abreu
email: labreu_at_gmail.com
EN blog:http://msmvps.com/blogs/luisabreu
Ok - but I but I need to change the color based on the value of data with in the recordset row. For example, this snippet below worked in .net 1.x versions, but it isn't working 2.0 In the code below I look up the value in
the "LobbyRoomDesc" label and change the background color in Cell(0) to either Red or Blue.
Thanks for your time and help!
Protected
Sub GridView_Lobby_RowDataBound(ByVal sender
As Object,
ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs)
Handles GridView_Lobby.RowDataBound
Dim x As
String = CType(e.Row.FindControl("LobbyRoomDesc"), System.Web.UI.WebControls.Label).Text
If x = "Lobby"
Then
e.Row.Cells(0).BackColor = System.Drawing.Color.FromName("Red")
Else
e.Row.Cells(0).BackColor = System.Drawing.Color.FromName("Blue")
End If
End Sub
The above code gives this error:
System.NullReferenceException was unhandled by user code
Message="Object reference not set to an instance of an object."
hello again.
hum, i think that you've forgot to perform a check to see if you're handling the header or footer of the grid (i think that in those cases it won't find the label because i think that you've only added them to the itemtemplates, or am i wrong)?
so, i'd change the code to something like this (it's in C#):
kbuchanan
Member
50 Points
10 Posts
Gridview - change cell background color
Oct 20, 2005 06:02 PM|LINK
I want to change the background color of a cell based on data within the row. BUT...I get the following error:
Databinding expressions are only supported on controls. System.Web.UI.WebControls.TemplateField is not a control.
When I attempt this:
<asp:TemplateField ControlStyle-CssClass='<%# Eval("VAdmDtTm") %>'>
I have tried code-behind on all the row events, but I guess this code (which worked in ASP.NET 1.x ibut is no longer valid in 2.0):
System.NullReferenceException was unhandled by user code
Message="Object reference not set to an instance of an object."
So - how can I set the cell background color based on data within the row?
Thanks!
Luis Abreu
All-Star
25674 Points
5369 Posts
MVP
Re: Gridview - change cell background color
Oct 21, 2005 01:26 PM|LINK
several options here. You can use a template or you can handle the rowdatabound event. if you handle the event, you can simply change the background of the cell with code similar to this one:
void Handle( object sender, GriViewRowEventArgs args )
{
args.Row.Cells[put the position of the cell here].BackColor = your color here;
}
Regards,
Luis Abreu
email: labreu_at_gmail.com
EN blog:http://msmvps.com/blogs/luisabreu
kbuchanan
Member
50 Points
10 Posts
Re: Gridview - change cell background color
Oct 22, 2005 03:52 AM|LINK
Ok - but I but I need to change the color based on the value of data with in the recordset row. For example, this snippet below worked in .net 1.x versions, but it isn't working 2.0 In the code below I look up the value in the "LobbyRoomDesc" label and change the background color in Cell(0) to either Red or Blue.
Sub GridView_Lobby_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView_Lobby.RowDataBoundThanks for your time and help!
Protected
Dim x As String = CType(e.Row.FindControl("LobbyRoomDesc"), System.Web.UI.WebControls.Label).Text
If x = "Lobby" Then
e.Row.Cells(0).BackColor = System.Drawing.Color.FromName("Red")
Else
e.Row.Cells(0).BackColor = System.Drawing.Color.FromName("Blue")
End If
End Sub
The above code gives this error:
System.NullReferenceException was unhandled by user code
Message="Object reference not set to an instance of an object."
Luis Abreu
All-Star
25674 Points
5369 Posts
MVP
Re: Gridview - change cell background color
Oct 24, 2005 12:17 PM|LINK
hum, i think that you've forgot to perform a check to see if you're handling the header or footer of the grid (i think that in those cases it won't find the label because i think that you've only added them to the itemtemplates, or am i wrong)?
so, i'd change the code to something like this (it's in C#):
if( e.Row.RowType != DataControlRowType.DataRow )
{
return;
}
//do the other stuff here...
Regards,
Luis Abreu
email: labreu_at_gmail.com
EN blog:http://msmvps.com/blogs/luisabreu