Asp.net Gridview : Replace null or empty value with default message - Free Asp.Net,MVC,AngularJs,Jquery,Javascript,Sql Server,WCF,Entity framework snippets and tutorial

Asp.net Gridview : Replace null or empty value with default message

In this article I am going to explain how to replace null or empty value with default message such as “-NA-“or “Data not available” in asp.net Gridview.

Description:
I am showing list of Employees in gridview data control. Some of columns are empty. I want to show a default message “-NA-“instead of empty column.

  
Implementation:

Method 1:

NullDisplayText Property of Gridview
NullDisplayText is used to show a predefined message/text if column is empty/null. This property is working in BoundField.

E.g.
<asp:BoundField HeaderText="Name" DataField="Name" NullDisplayText="-NA-">             </asp:BoundField>


Method 2:

If you are using TemplateField in that case check column is empty or not.

E.g.
  <asp:TemplateField HeaderText="Email">
                    <ItemTemplate>
                        <asp:Label ID="lblemail" runat="server" Text='<%# Eval("EmailId").ToString()==""?"-NA-" :Eval("EmailId") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>



*Note: In case of image you have to set NullImageUrl property in Imagefield. E.g.
<asp:ImageField DataImageUrlField="ImagePath" NullImageUrl="~/images/img-not-available.jpg" ControlStyle-Height="100" ControlStyle-Width="100"   >
<ControlStyle Height="100px" Width="100px"></ControlStyle>
                </asp:ImageField>


Complete HTML Markup of webform:

<head runat="server">
  <title>Replace null or empty value with default message </title>
</head>
<body>
    <form id="form1" runat="server">
    <div> 
         <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" OnRowDataBound="GridView1_RowDataBound1">
            <Columns>
                <asp:BoundField HeaderText="Name" DataField="Name" NullDisplayText="-NA-">
                <HeaderStyle HorizontalAlign="Left" />
                </asp:BoundField>              
                <asp:BoundField DataField="Phone" HeaderText="Phone" NullDisplayText="-NA-">
                <HeaderStyle HorizontalAlign="Left" />
                </asp:BoundField>
                <asp:BoundField DataField="Salary" HeaderText="Salary" NullDisplayText="-NA-">
                  <HeaderStyle HorizontalAlign="Left" />
                </asp:BoundField>
                  <asp:BoundField DataField="Department" HeaderText="Department" NullDisplayText="-NA-">
                <HeaderStyle HorizontalAlign="Left" />
                </asp:BoundField>
               <asp:TemplateField HeaderText="Email">
                    <ItemTemplate>
                        <asp:Label ID="lblemail" runat="server" Text='<%# Eval("EmailId").ToString()==""?"-NA-" :Eval("EmailId") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                 <asp:ImageField DataImageUrlField="ImagePath" NullImageUrl="~/images/img-not-available.jpg" ControlStyle-Height="100" ControlStyle-Width="100"   >
<ControlStyle Height="100px" Width="100px"></ControlStyle>
                </asp:ImageField>
            </Columns>
        </asp:GridView> 
        </div>
    </div>
    </form>
</body>
</html>


What do you think about this article?

If you found this article useful, please share and follow on Facebook, Twitter, Google Plus and other social media websites. To get free updates subscribe to newsletter. Please put your thoughts and feedback in comments section.

Share this

Share on FacebookTweet on TwitterPlus on Google+


EmoticonEmoticon