Solved

ASP.NET

Posted on 2010-11-09
6
557 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

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…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

809 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