2008-11-21 88 views
98

如何获取GridView控件以呈现<thead><tbody>标签?我知道.UseAccessibleHeaders使它把<th>而不是<td>,但我不能让<thead>出现。如何让Gridview呈现THEAD?

+0

仅供参考:默认情况下,UseAccessibleHeader为“true”,因此您无需设置它。 http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.useaccessibleheader。aspx – MikeTeeVee 2013-11-07 20:14:14

回答

168

这应做到:

gv.HeaderRow.TableSection = TableRowSection.TableHeader; 
+64

'HeaderRow`属性将是`null`直到`GridView`被数据绑定,所以一定要等到数据绑定发生之后再运行上面的代码。 – bdukes 2009-07-17 14:47:07

+4

至于下面的评论,至少在绑定ASP.NET 4.5后还不够晚 - 但它在OnPreRender中可行。 – philw 2013-08-06 11:16:57

+0

我有一个添加了自定义子标题的gridview。这些子标题中的每一个都显示来自数据源的数据。我想渲染`thead`的原因是在jQuery中使用它。但是在渲染标题之后,`tbody`似乎不可用。我的情况可能会丢失什么? – bonCodigo 2014-09-03 08:54:44

9

的答案代码需要去Page_LoadGridView_PreRender。我在Page_Load之后调用了一个方法,得到了一个NullReferenceException

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

我使用下面的代码要做到这一点:

我添加的陈述很重要。

否则(取决于你如何使你的网格),你会抛出异常一样:

表必须包含行区域的标题,正文的顺序,然后页脚。

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控件。

我实际上覆盖了Asp.net GridView来创建我自己的自定义控件,但是您可以将其粘贴到您的页面中,并通过名称引用GridView而不是使用custom-gridview方法。

仅供参考:我尚未测试页脚逻辑,但我确实知道这适用于标题。

3

这个工作对我来说:

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

14

OnRowDataBound事件中使用这样的:

if (e.Row.RowType == DataControlRowType.Header) 
    e.Row.TableSection = TableRowSection.TableHeader; 
1

我知道这是旧的,但是,这里的MikeTeeVee的答案的解释,对于一个标准的GridView:

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

    }