显示每行的gridview标题

时间:2014-05-02 06:23:54

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

我使用下面的代码在我的导出到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);
            }

1 个答案:

答案 0 :(得分:-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
        }
    }
}