?
Solved

ASP.NET

Posted on 2010-11-09
6
Medium Priority
?
564 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 2000 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …

649 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