In C# how do I still show the headers of a gridview, even with the data source is empty.
I am not auto generating the columns as they are all predefined.
C
Juste add ShowHeaderWhenEmpty property and set it at true
This solution works for me
You can set the ShowHeadersWhenNoRecords property of the ownertableview to true. aspx:
<asp:GridView ID="RadGrid2" runat="server" >
<MasterTableView ShowHeadersWhenNoRecords="true" >
Also when the datasource for the GridView is null(when no records), you can try setting it as shown below: c#:
if (GridView1.DataSource == null)
{
GridView1.DataSource = new string[] { };
}
GridView1.DataBind();
set "<asp:GridView AutoGenerateColumns="false" ShowHeaderWhenEmpty="true""
showheaderwhenEmpty
Property
Use an EmptyDataTemplate like below. When your DataSource has no records, you will see your grid with headers, and the literal text or HTML that is inside the EmptyDataTemplate tags.
<asp:GridView ID="gvResults" AutoGenerateColumns="False" HeaderStyle-CssClass="tableheader" runat="server">
<EmptyDataTemplate>
<asp:Label ID="lblEmptySearch" runat="server">No Results Found</asp:Label>
</EmptyDataTemplate>
<Columns>
<asp:BoundField DataField="ItemId" HeaderText="ID" />
<asp:BoundField DataField="Description" HeaderText="Description" />
...
</Columns>
</asp:GridView>
I found a very simple solution to the problem. I simply created two GridViews. The first GridView called a DataSource with a query that was designed to return no rows. It simply contained the following:
<Columns>
<asp:TemplateField HeaderStyle-HorizontalAlign="Left">
<HeaderTemplate>
<asp:Label ID="lbl0" etc.> </asp:Label>
<asp:Label ID="lbl1" etc.> </asp:Label>
</HeaderTemplate>
</asp:TemplateField>
</Columns>
Then I created a div with the following characteristics and I place a GridView inside of it with ShowHeader="false" so that the top row is the same size as all the other rows.
<div style="overflow: auto; height: 29.5em; width: 100%">
<asp:GridView ID="Rollup" runat="server" ShowHeader="false" DataSourceID="ObjectDataSource">
<Columns>
<asp:TemplateField HeaderStyle-HorizontalAlign="Left">
<ItemTemplate>
<asp:Label ID="lbl0" etc.> </asp:Label>
<asp:Label ID="lbl1" etc.> </asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
<asp:GridView ID="grdGroup" EmptyDataText="No Records Found" ShowHeaderWhenEmpty="True" runat="server">
This is a basic example of Gridview with EmptyDataText and ShowHeaderWhenEmpty