StateofDK
StateofDK

Reputation: 153

ShowHeaderWhenEmpty, EmptyDataText, and EmptyDataTemplate not working in gridview

Gridview populates properly when data is returned, however ShowHeaderWhenEmpty, EmptyDataText, and EmptyDataTemplate all do not work, even when no records are returned. I've read other solutions regarding CSSFriendly and some conflict where CSSFriendly disables the controls, however that doesn't seem to have solved the issue.

here is my 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>

Here is my code-behind

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

        }
}

Thanks in advance

Upvotes: 3

Views: 3220

Answers (1)

JustLearning
JustLearning

Reputation: 3322

For ShowHeaderWhenEmpty to work you must provide a not null datasource to the gridview control. Therefore, if your current code is providing null, one way to go around the problem is to create an empty datatable and bind it to your gridview

Upvotes: 3

Related Questions