如何让GridView.ShowHeaderWhenEmpty属性显示headers

How to get GridView.ShowHeaderWhenEmpty Property to display headers

我在我的 Asp.Net 应用程序中使用 .NET Framework 4.6.1 并尝试将 GridView.ShowHeaderWhenEmpty 属性 应用到我的 gridview 以在页面加载时显示 headers (数据table之前有数据填满行,为空)。当我加载此页面时,只有一个空白 space,直到用户单击其他一些控件。

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();
            }
        }
    }

我是否遗漏了某些内容,或者还有其他原因导致我的 headers 无法在页面加载时显示?

我终于意识到我必须创建一个空数据 table 并绑定它以便 ShowHeaderWhenEmpty 工作。在页面加载时,我将 null 参数(允许 null 参数)添加到我的 sp 中,这将 return dataset 我最终将用作我的 [=15] 的 Datasource =].这 return 编辑了一个空的 table 但允许 headers 显示。我还添加了空行(只是为了美观):

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();
            }
        }
     ...
}