Saturday, September 9, 2017

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>


No comments:

Post a Comment