• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 565
  • Last Modified:

ASP.NET

I am using a GridView control with ASP.NET and VB.NET.

I am populating the GridView with TemplateFields, sample below.  When I cycle through the rows in the RowDataBound event all fields are empty strings.  However, the grid is populating.  I just wanted to do some conditional formatting.


<Columns>
                         <asp:TemplateField HeaderText="Date" ItemStyle-Width="65">
                            <ItemTemplate>
                                <%# Format(Eval("AppointmentDate"), "MM/dd/yyyy")%>
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Time" ItemStyle-Width="50">
                            <ItemTemplate><%# Eval("AppointmentTime") & " " & Eval("AppointmentAMPM")%> </ItemTemplate>
                        </asp:TemplateField>
</Columns>


In the code behind......

        If e.Row.RowType = DataControlRowType.DataRow Then
            
                If e.Row.Cells(0).Text = "" Then
                    e.Row.BackColor = System.Drawing.Color.DarkGray
                 End If
        End If

Open in new window

0
dbote
Asked:
dbote
  • 3
  • 2
1 Solution
 
guru_samiCommented:
1--> 0 is the first column, so make sure your first column is not a button column.
2--> You can use DataRowView like below to check the value:

Dim drv As DataRowView = DirectCast(e.Row.DataItem, DataRowView)
If drv("AppointmentTime").ToString() == String.Empty Then
'''
End If

Check this guy: http://www.simple-talk.com/dotnet/asp.net/take-row-level-control-of-your-gridview/

0
 
BurniePCommented:
If you want custom formatting, I would use Labels to hold your value :
<Columns>
                         <asp:TemplateField HeaderText="Date" ItemStyle-Width="65">
                            <ItemTemplate>
                                <asp:Label id="AppointmentDate" runat="server" Text='<%# Format(Eval("AppointmentDate"), "MM/dd/yyyy")%'>
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Time" ItemStyle-Width="50">
                            <ItemTemplate><%# Eval("AppointmentTime") & " " & Eval("AppointmentAMPM")%> </ItemTemplate>
                        </asp:TemplateField>
</Columns>


In the code behind......

        If e.Row.RowType = DataControlRowType.DataRow Then
                If DirectCast(e.row.FindControl("AppointmentDate"), Label).Text = "" Then
                    e.Row.BackColor = System.Drawing.Color.DarkGray
                 End If
        End If
 

Open in new window

0
 
dboteAuthor Commented:
I do know that 0 is the first column and it is NOT a button column.  It is a Date field that displays perfectly.

When I run the code you provided I get a NullReferenceException was unhandled error. Code typed below.  I typed this code in the RowDataBound event.
Dim drv As DataRowView = DirectCast(e.Row.DataItem, DataRowView)
        If drv("AppointmentTime").ToString() <> String.Empty Then
           e.Row.Cells(0).BackColor = Drawing.Color.DarkGray
        End If

Open in new window

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
guru_samiCommented:
Inside the if block correct? can AppointmentTime be Null??
Try this:

If e.Row.RowType = DataControlRowType.DataRow Then
If drv("AppointmentTime") IsNot Nothing Or drv("AppointmentTime").ToString() <> String.Empty Then
           e.Row.Cells(0).BackColor = Drawing.Color.DarkGray
        End If
End If

You can also try FindControl as suggested by BurnieP.
0
 
dboteAuthor Commented:
Sami,

Thanks for leading me in the right direction.  When I used your code to create a datarow type....I realized that I am not using a datarow type but a custom class so that is what I used in the DirectCast statement and it worked.

Thank you for pushing me in the right direction.  I'm tired so I needed a shove.

Thank you.
0
 
dboteAuthor Commented:
Thanks for the link to the article too.  That will be usefull in the future.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now