How to display Null field in Gridview ?



Hi

So many time we will get situation to display to some user friendly message in gridview if there is no data available in database.

We can do like this

Step1: Populate the gridview using any method

Step2: Customize the bound field like this

<asp:BoundField DataField="EmpName" NullDisplayText="No Data" HeaderText="EmpName" />
<asp:BoundField DataField="EmpSal" HeaderText="EmpSal" NullDisplayText="No Data" />

Complete Gridview code like this

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" GridLines="Both"
CellPadding="4" ForeColor="#333333" Width="356px">
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:TemplateField HeaderText="SI.No">
<ItemTemplate>
<%#Container.DataItemIndex+1 %>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="EmpName" NullDisplayText="No Data" HeaderText="EmpName" />
<asp:BoundField DataField="EmpSal" HeaderText="EmpSal" NullDisplayText="No Data" />
</Columns>
<EditRowStyle BackColor="#7C6F57" />
<FooterStyle BackColor="#1C5E55" ForeColor="White" Font-Bold="True" />
<HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#E3EAEB" />
<SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#F8FAFA" />
<SortedAscendingHeaderStyle BackColor="#246B61" />
<SortedDescendingCellStyle BackColor="#D4DFE1" />
<SortedDescendingHeaderStyle BackColor="#15524A" />
</asp:GridView>

Advertisement

4 thoughts on “How to display Null field in Gridview ?

  1. Sumith November 21, 2013 / 12:08 pm

    Good One….

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.