pascalmartin
asked on
coloring gridview rows dynamically
Hi,
In my gridview I want to color in green the rows for which the fielddate value is today and in orange the rows for which the fielddate value is tomorrow and later. The problem is that the following code works fine when binding the gridview because the code can find the label "lblDel" but fail when I edit the gridview because it can't find the the textbox "txtDel".
If I change the label by the text box them the gridview binding will fail.
Any idea how to code this?
Here is part of my code:
<asp:TemplateField SortExpression="DeliveryDa te">
<ItemStyle wrap="false" Width="350px" />
<HeaderTemplate><asp:LinkB utton ID="LinkButton5" runat="server" Text="DeliveryDate" CommandName="sort" CommandArgument="DeliveryD ate" /></HeaderTemplate>
<ItemStyle HorizontalAlign="left" Wrap="false"></ItemStyle>
<ItemTemplate><asp:Label ID="lblDel" Text='<%#Eval("DeliveryDat e","{0:dd/ mm/yyyy}") %>' runat="server" Width="150px" /></ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtDel" Text='<%# Bind("DeliveryDate","{0:dd /mm/yyyy}" ) %>' runat="server" width="150px" />
</EditItemTemplate>
</asp:TemplateField>
Sub GridOrders_RowDataBound(By Val sender As Object, ByVal e As GridViewRowEventArgs)
'///coloring the rows//////
For Each row As GridViewRow In GridOrders.Rows 'Loop through the row of the gridview
Dim strCurrentDate As String = CType(DateAndTime.Today(). ToString, Date).Date 'Declare the current date
'Get the date from the gridview row and convert to date type
Dim lblDelDate As Date = CDate(CType(row.FindContro l("lblDel" ), Label).Text.ToString).Date
'Dim txtDelDate As Date = CDate(CType(row.FindContro l("txtDel" ), TextBox).Text.ToString).Da te
If lblDelDate = strCurrentDate Then
row.BackColor = Drawing.Color.Green
Else
row.BackColor = Drawing.Color.Orange
End If
Next
End Sub
In my gridview I want to color in green the rows for which the fielddate value is today and in orange the rows for which the fielddate value is tomorrow and later. The problem is that the following code works fine when binding the gridview because the code can find the label "lblDel" but fail when I edit the gridview because it can't find the the textbox "txtDel".
If I change the label by the text box them the gridview binding will fail.
Any idea how to code this?
Here is part of my code:
<asp:TemplateField SortExpression="DeliveryDa
<ItemStyle wrap="false" Width="350px" />
<HeaderTemplate><asp:LinkB
<ItemStyle HorizontalAlign="left" Wrap="false"></ItemStyle>
<ItemTemplate><asp:Label ID="lblDel" Text='<%#Eval("DeliveryDat
<EditItemTemplate>
<asp:TextBox ID="txtDel" Text='<%# Bind("DeliveryDate","{0:dd
</EditItemTemplate>
</asp:TemplateField>
Sub GridOrders_RowDataBound(By
'///coloring the rows//////
For Each row As GridViewRow In GridOrders.Rows 'Loop through the row of the gridview
Dim strCurrentDate As String = CType(DateAndTime.Today().
'Get the date from the gridview row and convert to date type
Dim lblDelDate As Date = CDate(CType(row.FindContro
'Dim txtDelDate As Date = CDate(CType(row.FindContro
If lblDelDate = strCurrentDate Then
row.BackColor = Drawing.Color.Green
Else
row.BackColor = Drawing.Color.Orange
End If
Next
End Sub
try to add event rowEditing and to but ur code there
ASKER
I got an error message of object not found
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.