ShowHeaderWhenEmpty,EmptyDataText和EmptyDataTemplate在gridview

时间:2016-03-02 06:36:39

标签: c# asp.net gridview

返回数据时,

Gridview会正确填充,但ShowHeaderWhenEmptyEmptyDataTextEmptyDataTemplate都不起作用,即使没有返回任何记录也是如此。我已经阅读了有关CSSFriendly的其他解决方案以及CSSFriendly禁用控件的一些冲突,但这似乎没有解决问题。

这是我的ASP gridview

<asp:GridView runat="server" class="table table-striped table-hover" ID="gvPendingRequestsEP" GridLines="None" AutoGenerateColumns="false">
    <Columns>
        <asp:BoundField DataField="submitted_by" HeaderText="submitted by" />
        <asp:BoundField DataField="date_submitted" HeaderText="date submitted" />
        <asp:BoundField DataField="card_number" HeaderText="Card Number" />
        <asp:BoundField DataField="beginning_date" HeaderText="Date" />
        <asp:BoundField DataField="beginning_time" HeaderText="time" />
        <asp:BoundField DataField="total_time" HeaderText="hours" />
        <asp:BoundField DataField="in_place_rank" HeaderText="Time/ Pay" />
        <asp:BoundField DataField="status_id" HeaderText="Status" />
        <asp:TemplateField>
            <HeaderTemplate>
                edit
            </HeaderTemplate>
            <ItemTemplate>
                <a href="EPRequest.aspx?id=<%#Eval("card_number") %>">edit</a>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

这是我的代码隐藏

protected void PopulateCardManagers(object sender, EventArgs e)
{
    string connstring = ConfigurationManager.ConnectionStrings["TimeHubDBCS"].ConnectionString;
        using (SqlConnection conn = new SqlConnection(connstring))
        {
            SqlCommand cmdFillEP = new SqlCommand();
            cmdFillEP = new SqlCommand("spSelectPendingRequestsEP", conn);
            cmdFillEP.CommandType = System.Data.CommandType.StoredProcedure;
            cmdFillEP.Parameters.Add("@UserId", SqlDbType.VarChar).Value = lblUserLoggedIn.Text;

            DataSet dsRequests = new DataSet();
            DataTable dtEPCards;

            dsRequests.Tables.Add("dtEPCards");
            dtEPCards = dsRequests.Tables[0];

            SqlDataAdapter daPendingRequestsEP = new SqlDataAdapter();
            daPendingRequestsEP.SelectCommand = cmdFillEP;

            conn.Open();

            try
            {
                daPendingRequestsEP.Fill(dtEPCards);

                gvPendingRequestsEP.DataSource = dtEPCards;
                gvPendingRequestsEP.DataBind();
            }
            catch (Exception ex)
            {
                PopupTitle = "error populating EP Card Manager: ";
                message = ex.Message;
                ClientScript.RegisterStartupScript(this.GetType(), "Popup", "ShowPopup('" + message + ex.Message + "');", true);
            }

        }
}

提前致谢

1 个答案:

答案 0 :(得分:3)

要使ShowHeaderWhenEmpty起作用,必须向gridview控件提供非null数据源。因此,如果您当前的代码提供null,解决此问题的一种方法是创建一个空的datatable并将其绑定到您的gridview