Search code examples
c#asp.netgridviewintegernullable

How can I find a Label value in my gridview when it is an integer data type


I have a gridview named gvcmi.
And on the DataBound event I want color a column based on its value. The column I want to color is bound to a column in my table in the database. That data type for that column is integer. Here is my snippet:

protected void gvcmi_DataBound(object sender, GridViewRowEventArgs e)
        {
            Label mylbl = (Label)e.Row.Cells[8].FindControl("lblStatusv");

            if ( mylbl.Text == "1")
                {
                    e.Row.Cells[8].BackColor = System.Drawing.Color.Green;
                    e.Row.Cells[8].ForeColor = System.Drawing.Color.White;
                }
        }

When this runs I get this error:

System.NullReferenceException Object reference not set to an instance of an object

I know this has to be due to the fact that I am not converting the integer to a string maybe? But I am not sure when to do the convert.

Here is the GridView code:

<asp:GridView id="gvcmi"
              runat="server"
              AutoGenerateColumns="false"
              BorderWidth="1px"
              BackColor="White"
              CellPadding="3"
              CellSpacing="2"
              BorderStyle="Solid"
              BorderColor="Black"
              GridLines="Both"
              Pager="30"
              OnRowDataBound="gvcmi_DataBound"
              OnRowCommand ="gvcmi_RowCommand"  
>

<Columns>

<asp:TemplateField HeaderText="Customer #" HeaderStyle-CssClass= "hdrBase" ItemStyle-CssClass="GridBase">
<ItemTemplate>
<asp:LinkButton id="lbcustNum" runat="server" Text='<%#(Eval("customerid"))%>' CommandName="GetData" CommandArgument='<%# Container.DataItemIndex %>'/>
</ItemTemplate>
</asp:TemplateField>

<asp:TemplateField HeaderText="Kana Name" HeaderStyle-CssClass= "hdrBase" ItemStyle-CssClass="GridBase">
<ItemTemplate>
<asp:Label id="lblNameKana" runat="server" Text='<%#(Eval("namekana"))%>'/>
</ItemTemplate>
</asp:TemplateField>

<asp:TemplateField HeaderText="Kanji Name" HeaderStyle-CssClass= "hdrBase" ItemStyle-CssClass="GridBase">
<ItemTemplate>
<asp:Label id="lblNameKanji" runat="server" Text='<%#(Eval("namekanji"))%>'/>
</ItemTemplate>
</asp:TemplateField>

<asp:TemplateField HeaderText="Gender" HeaderStyle-CssClass= "hdrBase" ItemStyle-CssClass="GridBase">
<ItemTemplate>
<asp:Label id="lblgender" runat="server" Text='<%#(Eval("gender"))%>'/>
</ItemTemplate>
</asp:TemplateField>

<asp:TemplateField HeaderText="Age" HeaderStyle-CssClass= "hdrBase" ItemStyle-CssClass="GridBase">
<ItemTemplate>
<asp:Label id="lblage" runat="server" Text='<%#(Eval("age"))%>'/>
</ItemTemplate>
</asp:TemplateField>

<asp:TemplateField HeaderText="Email" HeaderStyle-CssClass= "hdrBase" ItemStyle-CssClass="GridBase">
<ItemTemplate>
<asp:Label id="lblemail" runat="server" Text='<%#(Eval("email"))%>'/>
</ItemTemplate>
</asp:TemplateField>

<asp:TemplateField HeaderText="# Visits" HeaderStyle-CssClass= "hdrBase" ItemStyle-CssClass="GridBase">
<ItemTemplate>
<asp:Label id="lblnov" runat="server" Text='<%#(Eval("numberofvisits"))%>'/>
</ItemTemplate>
</asp:TemplateField>

<asp:TemplateField HeaderText="Latest Visit" HeaderStyle-CssClass= "hdrBase" ItemStyle-CssClass="GridBase">
<ItemTemplate>
<asp:Label id="lbllatestv" runat="server" Text='<%#(Eval("latestvisit", "{0: MMMM dd日 (ddd)}"))%>'/>
</ItemTemplate>
</asp:TemplateField>

<asp:TemplateField HeaderText="Visit Status" HeaderStyle-CssClass= "hdrBase" ItemStyle-CssClass="GridBase">
<ItemTemplate>
<asp:Label id="lblStatusv" runat="server" Text='<%#(Eval("visitstatus"))%>'/>
</ItemTemplate>
</asp:TemplateField>

</Columns>

</asp:GridView>

Solution

  • You can use a null conditional check ?. in case the object you are accessing the property of is null

    hence if ( mylbl?.Text == "1") won't throw the System.NullReferenceException, however the condition will not pass if mylbl is null

    Please see - https://csharp.today/c-6-features-null-conditional-and-and-null-coalescing-operators/ for more information.