【问题标题】:Show title for gridview for each row为每一行显示gridview的标题
【发布时间】:2014-05-02 06:23:53
【问题描述】:

我正在使用下面的代码在导出到 excel 时为我的 gridview 设置标题。现在我需要在导出到 excel 后为网格的每一行显示这个标题。

有什么建议可以实现吗?

            if (gv.ID == "GridView1")
            {
                TableRow title = new TableRow();
                title.BackColor = Color.Cyan;
                TableCell titlecell = new TableCell();
                titlecell.ColumnSpan = 3;
                Label lbl = new Label();
                lbl.Text = "XYZ REPORT 2014";
                titlecell.Controls.Add(lbl);
                title.Cells.Add(titlecell);
                table.Rows.Add(title);
            }
            if (gv.ID == "GridView2")
            {
                TableRow title = new TableRow();
                title.BackColor = Color.Yellow;
                TableCell titlecell = new TableCell();
                titlecell.ColumnSpan = 4;
                Label lbl = new Label();
                lbl.Text = "XYZ REPORT 2015";
                titlecell.Controls.Add(lbl);
                title.Cells.Add(titlecell);
                table.Rows.Add(title);
            }

【问题讨论】:

  • 这段代码写在页面的什么地方?
  • 有一个名为 RowDataBound 的 Gridview 事件,该事件会为绑定到 gridview 的每一行调用,因此如果您想为每一行运行一些功能,请使用该事件。

标签: c# asp.net .net gridview export-to-excel


【解决方案1】:

在网格视图的交替行中使用标题

protected void gv_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        if (e.Row.RowState == DataControlRowState.Alternate)
        {
            // Ur header row here
        }
    }
}

【讨论】:

  • 它不会出现在所有行中。此外,由于我已经有列,它也会覆盖这些值,它不应该。
猜你喜欢
  • 1970-01-01
  • 2015-07-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-05-01
  • 2013-05-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多