I am using .NET Framework 4.6.1
in my Asp.Net application and trying to apply the GridView.ShowHeaderWhenEmpty
property to my gridview to display the headers on page load (before the data table has data fill in the rows and is empty). When I load this page, there is just a blank space until the user clicks some other controls.
ASPX
<div class="col-md-12" style="overflow: auto; width: 1150px; max-height: 800px; height: 800px; border-style:solid; border-color: darkblue; border-width:thin;">
<asp:GridView ID="uxSearchGridView" runat="server" ShowHeaderWhenEmpty="true" CssClass="GridView" HeaderStyle-BackColor="#ADD8E6" BorderStyle="Solid" onRowDataBound="uxSearchGridView_RowDataBound" AutoGenerateColumns="False" OnSorting="uxSearchGridView_Sorting" BackColor="White" BorderColor="#D6D2D2" BorderWidth="1px" CellPadding="3" SelectedIndex="-1" DataKeyNames="TicketNumber" AllowSorting="True" Font-Size="Small" Width="100%" Visible="True" EnableModelValidation="True" style=" margin-top: 10px; margin-bottom: 10px;">
<Columns>
<asp:CommandField ShowSelectButton="True" SelectText="Details" ButtonType="Button" HeaderText="Select" />
<asp:BoundField DataField="ID" HeaderText="ID" SortExpression="ID" />
<asp:BoundField DataField="TicketNumber" HeaderText="Ticket Number" SortExpression="Ticket Number" />
<asp:BoundField DataField="Complexity" HeaderText="Complexity" SortExpression="Complexity" />
<asp:BoundField DataField="NatureOfInquiry" HeaderText="Nature of Inquiry" SortExpression="NatureOfInquiry" />
<asp:BoundField DataField="SMEResponseDetail" HeaderText="Response" SortExpression="SMEResponseDetail" />
</Columns>
<FooterStyle BackColor="White" ForeColor="#000066" />
<HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" />
<RowStyle ForeColor="#000066" />
<SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#F1F1F1" />
<SortedAscendingHeaderStyle BackColor="#007DBB" />
<SortedDescendingCellStyle BackColor="#CAC9C9" />
<SortedDescendingHeaderStyle BackColor="#00547E" />
</asp:GridView>
</div>
C#
protected void Page_Load(object sender, EventArgs e)
{
_dtMgr = new DataAccessManager()
string staffName = _dtMgr.GetStaffNameByUser(Session["UserNameSession"].ToString());
if (staffName == string.Empty)
{
//error
}
else
{
Session["StaffName"] = staffName;
if (!Page.IsPostBack)
{
uxSearchGridView.DataSource = null;
uxSearchGridView.DataBind();
}
}
}
Is there something I'm leaving out or is there another reason that my headers will not display on page load?
I finally realized that I had to create an empty data table and bind it in order for ShowHeaderWhenEmpty
to work. On page load, I added null parameters (that allowed null parameters) to my sp
that would return the dataset
I would eventually use as the Datasource
for my gridview
. This returned an empty table but allowed the headers display. I also added and empty row (just for aesthetic purposes):
protected void Page_Load(object sender, EventArgs e)
{
_searchDT = _dtMgr.GetTicketsByKeyword(uxKeywordTextBox.Text, null);
....
if (!Page.IsPostBack)
{
DataRow dr = null;
dr = _searchDT.NewRow();
_searchDT.Rows.Add();
uxSearchGridView.DataSource = _searchDT;
uxSearchGridView.DataBind();
}
}
...
}