How to add a static row to a gridview

旧城冷巷雨未停 提交于 2019-12-11 11:42:44

问题


ok so my goal is to add only 1 static row under the gridview headr EXAMPLE:

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

|----------------------Footer-------------------------------|

my hunch is its got something with RowDataBound but that's as far as i have got.

i guess i need to explain myself better : what i want to do is the equivalent to adding a new HeaderRow... thnx for all the helpers :D

FOUND THE ANSWER: after a lot of googling i found what i was looking for , on the asp

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

in the code behind

 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:


FOUND THE ANSWER: after a lot of googling i found what i was looking for , on the asp

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

in the code behind

 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:


As you mention, you can probably insert a row during the RowDataBound event, but that would be messy.

Depending on the data structure you bind to your grid, you may be able to insert your static row to your data structure. I've even some implementations where the query is modified to return a static row, so you end up with something like:

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

Instead, can you add your static row as part of your header template?

It's hard to answer this properly without seeing more your code.



来源:https://stackoverflow.com/questions/10400134/how-to-add-a-static-row-to-a-gridview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!