Solved

How do I use condition statements inside a gridView

Posted on 2009-05-11
3
263 Views
Last Modified: 2013-11-07
Hi Expers,
I am developing a website for my rugby team. This website has a Schedule page, on this page I have a gridView that has columns for date, is home, opponent, score, game type

The "score" column is an TemplateField and inside the ItemTemplate I have 3 labes:

1) lblPointsInFavor (points for the team)
2) lblPointsAgainst(points against the team)
3) winLoss (no text, this is where I will like to show a W or L depending on the other two labels)

The problem is that I've tried everthing that I can think of, google, etc. . to find the value of those labels so that on the RowDataBound event I can make a comparison and then give the winLoss label the appropiate "W" or "L"

I keep getting this annoying error: Object reference not set to an instance of an object.

I will paste the code below.

Thanks so much!

aspx page (GridView code)
=============================
<asp:GridView ID="gvSchedule" runat="server" AutoGenerateColumns="False" AllowSorting="true" DataKeyNames="intGameTypeID"
                  Width="425px" BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" 
                  BorderWidth="1px" CellPadding="4" ForeColor="Black" GridLines="Horizontal">
       <Columns>
        <asp:BoundField DataField="dtmGameStart" HtmlEncode="False" DataFormatString="{0:MMM dd, yyyy - hh:mm tt}" HeaderText="Date" />
        <asp:BoundField DataField="fIsHome" />
        <asp:HyperLinkField HeaderText="Opponent" DataNavigateUrlFields="strOpponentURL" DataNavigateUrlFormatString="{0}" Target="_blank" DataTextField="strOpponentName" />
        <asp:TemplateField HeaderText="Score"> 
            <ItemTemplate>
                <asp:Label ID="lblPointsInFavor" runat="server" Text='<%# Eval("intPointsFavor") %>' /> 
                - 
                <asp:Label ID="lblPointsAgainst" runat="server" Text='<%# Eval("intPointsAgainst") %>' />
                &nbsp;
                [<asp:Label ID="winLoss" runat="server" />]
            </ItemTemplate>
        </asp:TemplateField>
        <asp:BoundField Visible="true" DataField="strGameType" HeaderText="Game Type" />
       </Columns>
        <FooterStyle BackColor="#CCCC99" ForeColor="Black" />
        <SelectedRowStyle BackColor="#CC3333" Font-Bold="True" ForeColor="White" />
        <PagerStyle BackColor="White" ForeColor="Black" HorizontalAlign="Right" />
        <HeaderStyle BackColor="#333333" Font-Bold="True" ForeColor="White" />
    </asp:GridView>
 
 
=========================
aspx.vb code
==========================
If e.Row.RowType = DataControlRowType.DataRow Then
 
            'Dim lblPointsInFavor As Label = DirectCast(e.Row.FindControl("lblPointsInFavor"), Label)
            Dim lblPointsInFavor As Label = DirectCast(gvSchedule.FindControl("lblPointsInFavor"), Label)
            Dim lblPointsAgainst As Label = DirectCast(gvSchedule.FindControl("lblPointsAgainst"), Label)
            Dim winLoss As Label = DirectCast(gvSchedule.FindControl("winLoss"), Label)
 
            Dim intFor As Integer = Convert.ToInt16(lblPointsInFavor.Text)
            Dim intAgainst As Integer = Convert.ToInt16(lblPointsAgainst.Text)
 
            If intFor > intAgainst Then
                winLoss.Text = "W"
            Else
                winLoss.Text = "L"
            End If
 
 
        End If

Open in new window

0
Comment
Question by:vthunder70
[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
  • 2
3 Comments
 
LVL 18

Accepted Solution

by:
carlnorrbom earned 500 total points
ID: 24356050
Hi,

You need to change the FindControl operations to look like the top one which is commented out. You need to reference the datarow being bound in the current event through e.Row.FindControl:

            Dim lblPointsInFavor As Label = DirectCast(e.Row.FindControl("lblPointsInFavor"), Label)
            Dim lblPointsAgainst As Label = DirectCast(e.Row.FindControl("lblPointsAgainst"), Label)
            Dim winLoss As Label = DirectCast(e.Row.FindControl("winLoss"), Label)

/Carl.
0
 

Author Comment

by:vthunder70
ID: 24356178
Hi Carl,

I tried that but still gives me the same error =(
0
 

Author Comment

by:vthunder70
ID: 24356241
Sorry Carl,

It is working! thanks. I had commented out my code on the aspx page =P d'oh!

Txs!
0

Featured Post

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!

Question has a verified solution.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
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…

751 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