如何获取GridView.ShowHeaderWhenEmpty属性以显示标题

时间:2019-03-25 16:37:42

标签: c# header aspxgridview is-empty asp.net-4.6

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

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

我是否遗漏了某些东西,或者还有其他原因导致我的标题不会在页面加载时显示?

1 个答案:

答案 0 :(得分:0)

我终于意识到我必须创建一个空数据表并将其绑定,以使ShowHeaderWhenEmpty能够正常工作。在页面加载时,我向我的sp添加了空参数(允许空参数),该参数将返回dataset,而我最终将其用作Datasource的{​​{1}}。这将返回一个空表,但允许显示标题。我还添加了一个空行(仅出于美学目的):

gridview