Solved

ASP.NET

Posted on 2010-11-09
6
555 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 41

Expert Comment

by:guru_sami
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks for the link to the article too.  That will be usefull in the future.
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

771 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

10 Experts available now in Live!

Get 1:1 Help Now