C# 如何让 Gridview 渲染 THEAD?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/309101/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I get Gridview to render THEAD?
提问by Andrew Bullock
How do I get the GridView
control to render the <thead>
<tbody>
tags? I know .UseAccessibleHeaders
makes it put <th>
instead of <td>
, but I cant get the <thead>
to appear.
如何获得GridView
渲染<thead>
<tbody>
标签的控件?我知道.UseAccessibleHeaders
让它 put<th>
而不是<td>
,但我无法让它<thead>
出现。
采纳答案by Phil Jenkins
This should do it:
这应该这样做:
gv.HeaderRow.TableSection = TableRowSection.TableHeader;
回答by ASalvo
The code in the answer needs to go on Page_Load
or GridView_PreRender
. I put it in a method that was called after Page_Load
and got a NullReferenceException
.
答案中的代码需要继续Page_Load
或GridView_PreRender
。我把它放在一个被调用的方法中,Page_Load
并得到了一个NullReferenceException
.
回答by Rajpurohit
Create a function and use that function in your PageLoad
event like this:
创建一个函数并在您的PageLoad
事件中使用该函数,如下所示:
The function is:
功能是:
private void MakeGridViewPrinterFriendly(GridView gridView) {
if (gridView.Rows.Count > 0) {
gridView.UseAccessibleHeader = true;
gridView.HeaderRow.TableSection = TableRowSection.TableHeader;
}
}
The PageLoad
event is:
该PageLoad
事件是:
protected void Page_Load(object sender, EventArgs e) {
if (!IsPostBack)
{
MakeGridViewPrinterFriendly(grddata);
}
}
回答by MikeTeeVee
I use the following code to do this:
我使用以下代码来做到这一点:
The if
statements I added are important.
if
我添加的陈述很重要。
Otherwise (depending on how you render your grid) you'll throw exceptions like:
否则(取决于你如何渲染你的网格)你会抛出如下异常:
The table must contain row sections in order of header, body and then footer.
表格必须包含按页眉、正文和页脚顺序排列的行部分。
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);
}
The this
object is my GridView.
该this
对象是我的 GridView。
I actually overrode the Asp.net GridView to make my own custom control, but you could paste this into your aspx.cspage and reference the GridView by name instead of using the custom-gridview approach.
我实际上覆盖了 Asp.net GridView 来制作我自己的自定义控件,但是您可以将其粘贴到您的aspx.cs页面中并按名称引用 GridView,而不是使用自定义 gridview 方法。
FYI: I haven't tested the footer logic, but I do know this works for Headers.
仅供参考:我还没有测试过页脚逻辑,但我知道这适用于页眉。
回答by Felipe Delgado
This works for me:
这对我有用:
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;
}
}
This was tried in VS2010.
这是在 VS2010 中尝试过的。
回答by Neto Kuhn
I use this in OnRowDataBound
event:
我在OnRowDataBound
事件中使用它:
protected void GridViewResults_OnRowDataBound(object sender, GridViewRowEventArgs e) {
if (e.Row.RowType == DataControlRowType.Header) {
e.Row.TableSection = TableRowSection.TableHeader;
}
}
回答by Jonathan Harris
I know this is old, but, here's an interpretation of MikeTeeVee's answer, for a standard gridview:
我知道这是旧的,但是,这是对标准网格视图的 MikeTeeVee 答案的解释:
aspx page:
aspx页面:
<asp:GridView ID="GridView1" runat="server"
OnPreRender="GridView_PreRender">
aspx.cs:
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;
}
}
回答by Michael
You can also use jQuery to add it. This avoids the problem with TableRowSection.TableHeader where gets dropped on PostBack.
您也可以使用 jQuery 来添加它。这避免了 TableRowSection.TableHeader 在 PostBack 时被丢弃的问题。
$('#myTableId').prepend($("<thead></thead>").append($(this).find("#myTableId tr:first")));
$('#myTableId').prepend($("<thead></thead>").append($(this).find("#myTableId tr:first")));