Solved

ASP.NET

Posted on 2010-11-09
6
556 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
  • 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…

911 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now