1

I am using a Gridview with datasource is a List. How can I show the header if the List is null or for a empty gridview?

Sauron
  • 16,668
  • 41
  • 122
  • 174
  • Possible duplicate of [GridView - Show headers on empty data source](https://stackoverflow.com/questions/354369/gridview-show-headers-on-empty-data-source) –  Jul 01 '17 at 14:43

3 Answers3

3

if you are using fx4.0 then Set ShowHeaderWhenEmpty to true in Grid view

please look at below code

asp:GridView ID="gvPreview" runat="server" ShowHeaderWhenEmpty="True" 
shanethehat
  • 15,460
  • 11
  • 57
  • 87
0

You can use HeaderTemplate property to setup the head programatically or use ListView instead if you are using .NET 3.5.

or

You can even try below

//if data exists

if (dtSource.Rows.Count != 0)
{
grdView.DataSource = dtSource;
grdView.DataBind();
}
else
{
 //Other wise add a emtpy "New Row" to the datatable and then hide it after binding.
 dtFunding.Rows.Add(dtSource.NewRow());
 grdView.DataSource = dtSource;
 grdView.DataBind();
 grdView.Rows[0].Visible = false;
}
Sauron
  • 16,668
  • 41
  • 122
  • 174
Ravi Vanapalli
  • 9,805
  • 3
  • 33
  • 43