Solved

coloring gridview rows dynamically

Posted on 2014-11-05
3
252 Views
Last Modified: 2014-12-01
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="DeliveryDate">
<ItemStyle wrap="false" Width="350px" />
<HeaderTemplate><asp:LinkButton ID="LinkButton5" runat="server" Text="DeliveryDate" CommandName="sort" CommandArgument="DeliveryDate"  /></HeaderTemplate>
<ItemStyle HorizontalAlign="left" Wrap="false"></ItemStyle>
<ItemTemplate><asp:Label ID="lblDel" Text='<%#Eval("DeliveryDate","{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(ByVal 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.FindControl("lblDel"), Label).Text.ToString).Date
            'Dim txtDelDate As Date = CDate(CType(row.FindControl("txtDel"), TextBox).Text.ToString).Date

            If lblDelDate = strCurrentDate Then
                row.BackColor = Drawing.Color.Green
            Else
                row.BackColor = Drawing.Color.Orange
            End If
        Next
End Sub
0
Comment
Question by:pascalmartin
3 Comments
 
LVL 16

Expert Comment

by:Kamal Khaleefa
ID: 40425620
try to add event rowEditing and to but ur code there
0
 

Author Comment

by:pascalmartin
ID: 40425656
I got an error message of object not found
0
 
LVL 14

Accepted Solution

by:
CtrlAltDl earned 500 total points
ID: 40448442
You don't need to  loop trough each row since RowDataBound is called after each row.

Use e.Row.RowType to check if it is a data row and not a header or footer.  Use e.Row.RowState to see if the gridview is in edit mode.

 Sub GridOrders_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
        '///coloring the rows//////
        //Check if it is a data row and not a header/footer and check if it is in the Edit mode
        If e.Row.RowType = DataControlRowType.DataRow And e.Row.RowState <> DataControlRowState.Edit Then
            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.FindControl("lblDel"), Label).Text.ToString).Date
            'Dim txtDelDate As Date = CDate(CType(row.FindControl("txtDel"), TextBox).Text.ToString).Date

            If lblDelDate = strCurrentDate Then
                row.BackColor = Drawing.Color.Green
            Else
                row.BackColor = Drawing.Color.Orange
            End If
        End If
End Sub 

Open in new window

0

Featured Post

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question