代码之家  ›  专栏  ›  技术社区  ›  Andrew Bullock

如何让GridView呈现Thead?

  •  104
  • Andrew Bullock  · 技术社区  · 16 年前

    我怎么得到 GridView 控件以呈现 <thead> <tbody> 标签?我知道 .UseAccessibleHeaders 使它成为 <th> 而不是 <td> 但是我不能得到 <泰德& GT; 出现。

    7 回复  |  直到 7 年前
        1
  •  178
  •   stefan    7 年前

    应该这样做:

    gv.HeaderRow.TableSection = TableRowSection.TableHeader;
    
        2
  •  19
  •   stefan    7 年前

    我用这个 OnRowDataBound 事件:

    if (e.Row.RowType == DataControlRowType.Header)
        e.Row.TableSection = TableRowSection.TableHeader;
    
        3
  •  10
  •   mjhm    11 年前

    答案中的代码需要继续 Page_Load GridView_PreRender . 我把它放在一个在 页负荷 得到了一个 NullReferenceException .

        4
  •  7
  •   stefan    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);
    }
    

    这个 this 对象是我的网格视图。

    实际上,我超越了ASP.NET GridView来创建自己的自定义控件,但您可以将其粘贴到 在后台 按名称分页并引用网格视图,而不是使用自定义网格视图方法。

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

        5
  •  4
  •   irfandar    7 年前

    这对我很有用:

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

        6
  •  2
  •   stefan    7 年前

    创建一个函数并在 PageLoad 类似事件:

    功能是:

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

    这个 页页 事件是:

    protected void Page_Load(object sender, EventArgs e) {
            if (!IsPostBack)
            {
                MakeGridViewPrinterFriendly(grddata);
            }
    }
    
        7
  •  1
  •   Jonathan Harris    8 年前

    我知道这很古老,但对于标准的网格视图,以下是对Miketeeve答案的解释:

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