118

如何让GridView控件呈现<thead> <tbody>标签?我知道.UseAccessibleHeaders让它 put<th>而不是<td>,但我不能让它<thead>出现。

4

8 回答 8

195

这应该这样做:

gv.HeaderRow.TableSection = TableRowSection.TableHeader;
于 2008-11-21T15:34:00.467 回答
29

我在OnRowDataBound事件中使用它:

protected void GridViewResults_OnRowDataBound(object sender, GridViewRowEventArgs e) {
    if (e.Row.RowType == DataControlRowType.Header) {
        e.Row.TableSection = TableRowSection.TableHeader;
    }
}
于 2015-01-08T18:45:34.503 回答
10

答案中的代码需要继续Page_Loador GridView_PreRender。我把它放在一个被调用的方法中Page_Load并得到一个NullReferenceException.

于 2009-04-30T20:16:42.753 回答
7

我使用以下代码来执行此操作:

我添加的if陈述很重要。

否则(取决于你如何渲染你的网格)你会抛出异常,比如:

表格必须包含按页眉、正文和页脚顺序排列的行部分。

protected override void OnPreRender(EventArgs e)
{
    if ( (this.ShowHeader == true && this.Rows.Count > 0)
      || (this.ShowHeaderWhenEmpty == true))
    {
        //Force GridView to use <thead> instead of <tbody> - 11/03/2013 - MCR.
        this.HeaderRow.TableSection = TableRowSection.TableHeader;
    }
    if (this.ShowFooter == true && this.Rows.Count > 0)
    {
        //Force GridView to use <tfoot> instead of <tbody> - 11/03/2013 - MCR.
        this.FooterRow.TableSection = TableRowSection.TableFooter;
    }
    base.OnPreRender(e);
}

对象是我的thisGridView。

我实际上覆盖了 Asp.net GridView 来制作我自己的自定义控件,但是您可以将其粘贴到您的aspx.cs页面中并按名称引用 GridView 而不是使用自定义网格视图方法。

仅供参考:我没有测试页脚逻辑,但我知道这适用于页眉。

于 2013-11-07T20:30:28.840 回答
5

这对我有用:

protected void GrdPagosRowCreated(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        e.Row.TableSection = TableRowSection.TableBody;
    }
    else if (e.Row.RowType == DataControlRowType.Header)
    {
        e.Row.TableSection = TableRowSection.TableHeader;
    }
    else if (e.Row.RowType == DataControlRowType.Footer)
    {
        e.Row.TableSection = TableRowSection.TableFooter;
    }
}

这是在VS2010中尝试过的。

于 2014-06-23T19:01:22.840 回答
3

我知道这很旧,但是,对于标准的网格视图,这是对 MikeTeeVee 答案的解释:

页面:

<asp:GridView ID="GridView1" runat="server" 
    OnPreRender="GridView_PreRender">

aspx.cs:

    protected void GridView_PreRender(object sender, EventArgs e)
    {
        GridView gv = (GridView)sender;

        if ((gv.ShowHeader == true && gv.Rows.Count > 0)
            || (gv.ShowHeaderWhenEmpty == true))
        {
            //Force GridView to use <thead> instead of <tbody> - 11/03/2013 - MCR.
            gv.HeaderRow.TableSection = TableRowSection.TableHeader;
        }
        if (gv.ShowFooter == true && gv.Rows.Count > 0)
        {
            //Force GridView to use <tfoot> instead of <tbody> - 11/03/2013 - MCR.
            gv.FooterRow.TableSection = TableRowSection.TableFooter;
        }

    }
于 2016-11-22T17:13:29.423 回答
2

创建一个函数并在您的PageLoad事件中使用该函数,如下所示:

功能是:

private void MakeGridViewPrinterFriendly(GridView gridView) {  
    if (gridView.Rows.Count > 0) {          
        gridView.UseAccessibleHeader = true;  
        gridView.HeaderRow.TableSection = TableRowSection.TableHeader;  
    }  
} 

事件PageLoad是:

protected void Page_Load(object sender, EventArgs e) {
        if (!IsPostBack)
        {
            MakeGridViewPrinterFriendly(grddata);
        }
}
于 2013-04-05T09:26:47.793 回答
0

您也可以使用 jQuery 来添加它。这避免了 TableRowSection.TableHeader 在 PostBack 上被丢弃的问题。

$('#myTableId').prepend($("<thead></thead>").append($(this).find("#myTableId tr:first")));

于 2019-07-26T18:16:54.370 回答