JG Vimalan's Blog

It is all about coding!

Always display GridView header

In ASP.NET 4.0 GridView, Microsoft has added a cool property called ‘ShowHeaderWhenEmpty’. Using this property is very simple.  According to the description of this property, the header will be displayed only if you have <EmptyDataTemplate> set in your gridview. Header will not be displayed only just by setting the ‘ShowHeaderWhenEmpty’ property to true.  My sample gridview is given below,

<asp:GridView ID=”UserInformationViewer” runat=”server”
ForeColor=”#333333″ CellPadding=”4″ ShowHeaderWhenEmpty=”True”
style=”margin-right: 0px” AutoGenerateColumns=”False”>
<AlternatingRowStyle BackColor=”White” ForeColor=”#284775″ />
<EditRowStyle BackColor=”#999999″ />
<FooterStyle BackColor=”#5D7B9D” Font-Bold=”True” ForeColor=”White” />
<HeaderStyle BackColor=”#5D7B9D” Font-Bold=”True” ForeColor=”White” />
<PagerStyle BackColor=”#284775″ ForeColor=”White” HorizontalAlign=”Center” />
<RowStyle BackColor=”#F7F6F3″ ForeColor=”#333333″ />
<SelectedRowStyle BackColor=”#E2DED6″ Font-Bold=”True” ForeColor=”#333333″ />
<SortedAscendingCellStyle BackColor=”#E9E7E2″ />
<SortedAscendingHeaderStyle BackColor=”#506C8C” />
<SortedDescendingCellStyle BackColor=”#FFFDF8″ />
<SortedDescendingHeaderStyle BackColor=”#6F8DAE” />
<Columns>
<asp:BoundField HeaderText=”Name” ShowHeader=”true” DataField=”Name” />

<asp:BoundField HeaderText=”Age” ShowHeader=”true” DataField=”Age” />

<asp:BoundField HeaderText=”Gender” ShowHeader=”true” DataField=”Gender” />

<asp:BoundField HeaderText=”DOB” ShowHeader=”true” DataField=”DOB” />
</Columns>
<EmptyDataTemplate>
            <p>No record found.</p>
            </EmptyDataTemplate>
</asp:GridView>

Now, I have a method for setting datasource to this gridview.

private DataSet MockResult(bool addrows)
{
DataTable dt = new DataTable(“User Information”);
DataColumn c1 = new DataColumn(“Name”);
DataColumn c2 = new DataColumn(“Age”);
DataColumn c3 = new DataColumn(“Gender”);
DataColumn c4 = new DataColumn(“DOB”);
dt.Columns.Add(c1);
dt.Columns.Add(c2);
dt.Columns.Add(c3);
dt.Columns.Add(c4);

if (addrows)
{
for (int i = 0; i < 1000; i++)
{
DataRow r = dt.NewRow();
r[c1] = “Name” + i;
r[c2] = “Age” + i;
r[c3] = “Gender” + i;
r[c4] = DateTime.Now.ToShortDateString();

dt.Rows.Add(r);
}
}

DataSet ds = new DataSet();
ds.Tables.Add(dt);

return ds;
}

Now, I am setting the datasource for this gridview as shown below. The key thing here is,  do not set null value to the gridview datasource.  Even if there is no rows,  make sure that you have columns defined in the datasource object.

protected void RunButton_Click(object sender, EventArgs e)
{

//By passing false in the method parameter, I am just generating the columns and not the rows.
UserInformationViewer.DataSource = MockResult(false);
UserInformationViewer.DataBind();
}

Now, I can see the headers even if there is no data,

 

Advertisements

August 6, 2012 - Posted by | ASP.NET, C#.NET

1 Comment »

  1. Thanks for sharing, but for some reason I could not still able to display header when there is no records present, where there are no records grid is showing No records found. but without header.

    Comment by Ram | January 4, 2015 | Reply


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: