Thursday, October 25, 2012

Handle Null Value within Gridview




Try these to handle Null Values with Gridview

1) With TemplateField :
   <asp:TemplateField>
      <ItemTemplate>
            <asp:Label ID="TxtPhn" runat="server"
                                  Text='<%# Eval("Phone")== Null ? "N / A" : Eval("Phone") %>'>
            </asp:Label>
     </ItemTemplate>
</asp:TemplateField>


2) With BoundField :

<asp:BoundField NullDisplayText="N / A" DataField="Phone" />


3) Another Way :

<span><%#Eval("Phone1") == null ? "it is null" : Eval("Phone1")%></span>


4) Another Way :


public string checkForNull(object val)
{
    if (val == null)
       {
          return "null value";
        }
   return val.ToString();
}

and in .aspx Page
<span><%#checkForNull(Eval("Phone1"))%></span>

0 comments:

Post a Comment

Twitter Delicious Facebook Digg Stumbleupon Favorites More