我如何让 Gridview 渲染 THEAD?

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

63579 次浏览

这个应该可以:

gv.HeaderRow.TableSection = TableRowSection.TableHeader;

答案中的代码需要在 Page_LoadGridView_PreRender上运行。我把它放在 Page_Load之后调用的方法中,得到了 NullReferenceException

创建一个函数并在 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);
}
}

我使用以下代码来完成这项工作:

我添加的 if语句非常重要。

Otherwise (depending on how you render your grid) you'll throw exceptions like:

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

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

this对象是我的 GridView。

I actually overrode the Asp.net GridView to make my own custom control, but you could paste this into your Aspx.cs page and reference the GridView by name instead of using the custom-gridview approach.

仅供参考: 我还没有测试页脚逻辑,但是我知道这对 Header 很有用。

这对我有用:

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中尝试的。

我在 OnRowDataBound事件中使用了这个:

protected void GridViewResults_OnRowDataBound(object sender, GridViewRowEventArgs e) {
if (e.Row.RowType == DataControlRowType.Header) {
e.Row.TableSection = TableRowSection.TableHeader;
}
}

我知道这很老套,但是,以下是对 MikeTeeVee 的回答的一个解释,对于一个标准的网格视图:

Aspx 页面:

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


}

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

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