Solved

ASP.NET

Posted on 2010-11-09
6
561 Views
Last Modified: 2013-11-07
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
Comment
Question by:dbote
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 41

Accepted Solution

by:
guru_sami earned 500 total points
ID: 34097793
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
 
LVL 16

Expert Comment

by:BurnieP
ID: 34097940
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
 
LVL 1

Author Comment

by:dbote
ID: 34097979
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
Technology Partners: 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!

 
LVL 41

Expert Comment

by:guru_sami
ID: 34098007
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
 
LVL 1

Author Comment

by:dbote
ID: 34098050
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
 
LVL 1

Author Closing Comment

by:dbote
ID: 34098054
Thanks for the link to the article too.  That will be usefull in the future.
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

732 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