如何将静态行添加到gridview

时间:2012-05-01 15:40:07

标签: c# asp.net static row aspxgridview

好的,所以我的目标是在gridview headr下只添加一个静态行示例:

| coloumnHeader1 | coloumnHeader2 | coloumnHeader3 | coloumnHeader4 |

| ---------------------- static Row ---------------------- ----- | | DataBoundField | DataBoundField | DataBoundField | DataBoundField | | DataBoundField | DataBoundField | DataBoundField | DataBoundField | | DataBoundField | DataBoundField | DataBoundField | DataBoundField | | DataBoundField | DataBoundField | DataBoundField | DataBoundField | | DataBoundField | DataBoundField | DataBoundField | DataBoundField | | DataBoundField | DataBoundField | DataBoundField | DataBoundField |

| ----------------------页脚----------------------- -------- |

我的预感是它与RowDataBound有关,但这就是我所拥有的。

我想我需要更好地解释自己:我想要做的就是相当于添加一个新的HeaderRow ...... thnx为所有助手:D

找到答案: 经过大量的谷歌搜索,我找到了我想要的东西, 在asp上

<asp:GridView OnPreRender="grd_Pre" CssClass="table" ID="GridView1" runat="server" AutoGenerateColumns="False" 
   >

中的代码

 protected void grd_Pre(object sender, EventArgs e)
{
    GridViewRow gv = new GridViewRow(0, 0, DataControlRowType.Header, DataControlRowState.Normal);
    TableCell tc = new TableCell();
    tc.ColumnSpan = 3;
    tc.Text = "GridView Header";
    tc.Attributes.Add("style", "text-align:center");
    gv.Cells.Add(tc);
    this.GridView1.Controls[0].Controls.AddAt(0, gv);
}

2 个答案:

答案 0 :(得分:2)

找到答案: 经过大量的谷歌搜索,我找到了我想要的东西, 在asp上

<asp:GridView OnPreRender="grd_Pre" CssClass="table" ID="GridView1" runat="server" AutoGenerateColumns="False" 
   >

中的代码

 protected void grd_Pre(object sender, EventArgs e)
{
    GridViewRow gv = new GridViewRow(0, 0, DataControlRowType.Header, DataControlRowState.Normal);
    TableCell tc = new TableCell();
    tc.ColumnSpan = 3;
    tc.Text = "GridView Header";
    tc.Attributes.Add("style", "text-align:center");
    gv.Cells.Add(tc);
    this.GridView1.Controls[0].Controls.AddAt(0, gv);
}

答案 1 :(得分:0)

正如您所提到的,您可以在RowDataBound事件期间插入一行,但这会很混乱。

根据绑定到网格的数据结构,您可以将静态行插入数据结构。我甚至在一些修改查询以返回静态行的实现中,所以最终得到的结果如下:

select field1, ... , fieldN from table
union
select 'static 1', ...

相反,您可以将静态行添加为标题模板的一部分吗?

如果没有看到更多代码,就很难正确回答这个问题。