GridView不显示列或空记录文本

时间:2015-12-15 20:17:09

标签: c# asp.net gridview

我有一个存储过程,它返回包含列名的数据。下面是空存储过程返回的内容。

enter image description here

我的gridview仅在返回数据时显示列标题。

当数据表为0时, NOT 显示列标题(ShowHeaderWhenEmpty)或找不到记录(EmptyDataText)

protected void populateTableData(GridView gv, DataTable dt) {
            //CLEAR ANY PREVIOUS DATA
            gv.DataSource = null;
            gv.DataBind();

            //LOAD NEW DATA
            gv.DataSource = dt;
            gv.DataBind();
            gv.ShowHeaderWhenEmpty = true; 
            gv.EmptyDataText = "No records where found.";

            //REQUIRED FOR BOOTSTRAP TO MAKE THE TABLE LOOK CORRECTLY
            gv.UseAccessibleHeader = true;

        }

这是我在aspx方面的gridview:

<asp:GridView runat="server" ID="gvFees" GridLines="None" CellPadding="0" CellSpacing="0" AutoGenerateColumns="true"  CssClass="table table-hover table-striped" OnRowDataBound="gvFees_RowDataBound" ></asp:GridView>

我做错了什么?我更喜欢代码隐藏解决方案。

1 个答案:

答案 0 :(得分:1)

DataBind()电话移至结尾。在设置显示标题的选项之前绑定数据。

protected void populateTableData(GridView gv, DataTable dt)
{
    //CLEAR ANY PREVIOUS DATA
    gv.DataSource = null;
    gv.DataBind();

    //LOAD NEW DATA
    gv.DataSource = dt;
    gv.ShowHeaderWhenEmpty = true;
    gv.EmptyDataText = "No records where found.";

    //REQUIRED FOR BOOTSTRAP TO MAKE THE TABLE LOOK CORRECTLY
    gv.UseAccessibleHeader = true;
    gv.DataBind();
}