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>

Advertisements

4 Responses to “How to display Null field in Gridview ?”

  1. ...Marquito Says:

    thanks, mate.

    • Chandra Dev Says:

      You are welcome.

  2. Sumith Says:

    Good One….

    • Chandra Dev Says:

      Thank you.


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 )

Google+ photo

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

Connecting to %s

%d bloggers like this: