How can i make GridView Header dynamic?

  • Thread starter Thread starter rote
  • Start date Start date
R

rote

I would like to make my Gridview header dynmic i.e databinded from
database..
I want my users to be able to change the headers on the fly..
Any ideas?
 
I would like to make my Gridview header dynmic i.e databinded from
database..
I want my users to be able to change the headers on the fly..
Any ideas?

Hi,
We had a similar kind of requirement. We created a new header
above the GridView header. For that, we added new GirdViewRow with
proper cells (columns) and data, and it worked. You may use the same.
I dont know whether it solves your complete problem, but you will need
to create new GridViewRow for adding some header above the GridView's
default header.

Thanks,
coolCoder
 
I would like to make my Gridview header dynamic i.e databound from
database..
I want my users to be able to change the headers on the fly..

Can you clarify what you mean, please...

Are you saying that you want your users to be able to choose which columns
are displayed, or just to be able to change the text displayed at the top of
each column...?

If the latter, what happens if e.g. your GridView has four columns and they
specify the same header for all of them...?
 
Mark thanks for the reply.
The header colmuns would be fixed so lets say 4 and thats it.
Just to be able to change the text displayed on top of each column.
 
The header colmuns would be fixed so lets say 4 and thats it.
Just to be able to change the text displayed on top of each column.

<asp:GridView ID="MyGridView" runat="server"
OnRowDataBound="MyGridView_RowDataBound" AutoGenerateColumns="False">

protected void MyGridView_RowDataBound(object sender, GridViewRowEventArgs
e)
{
if (e.Row.RowType == DataControlRowType.Header)
{
e.Row.Cells[0].Text = "First";
e.Row.Cells[1].Text = "Second";
e.Row.Cells[2].Text = "Third";
e.Row.Cells[3].Text = "Fourth";
}
}
 
Thanks CoolCoder for the reply.
But can you explain how you added the GridViewRow and placed it at the
header.Some snippet code,resource etc
My GridView is using a TemplateField
<asp:TemplateField HeaderText="Year">
<ItemTemplate/>
</asp:TemplateField>
I tried databinding the data on the HeaderText above but doesn't like it...
 
hmm.. thanks Mark
I have already started creating a GridViewRow under the same event.
But your snippet looks like less code and i can just loop through e.Rows and
insert my values
Let me see how it goes...

Mark Rae said:
The header colmuns would be fixed so lets say 4 and thats it.
Just to be able to change the text displayed on top of each column.

<asp:GridView ID="MyGridView" runat="server"
OnRowDataBound="MyGridView_RowDataBound" AutoGenerateColumns="False">

protected void MyGridView_RowDataBound(object sender, GridViewRowEventArgs
e)
{
if (e.Row.RowType == DataControlRowType.Header)
{
e.Row.Cells[0].Text = "First";
e.Row.Cells[1].Text = "Second";
e.Row.Cells[2].Text = "Third";
e.Row.Cells[3].Text = "Fourth";
}
}
 
Mark Rae said:
I would like to make my Gridview header dynamic i.e databound from
database..
I want my users to be able to change the headers on the fly..

Can you clarify what you mean, please...

Are you saying that you want your users to be able to choose which
columns are displayed, or just to be able to change the text displayed
at the top of each column...?

If the latter, what happens if e.g. your GridView has four columns and
they specify the same header for all of them...?

The header colmuns would be fixed so lets say 4 and thats it.
Just to be able to change the text displayed on top of each column.

<asp:GridView ID="MyGridView" runat="server"
OnRowDataBound="MyGridView_RowDataBound" AutoGenerateColumns="False">

protected void MyGridView_RowDataBound(object sender,
GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header)
{
e.Row.Cells[0].Text = "First";
e.Row.Cells[1].Text = "Second";
e.Row.Cells[2].Text = "Third";
e.Row.Cells[3].Text = "Fourth";
}
}

I have already started creating a GridViewRow under the same event.
But your snippet looks like less code and i can just loop through e.Rows
and insert my values
Let me see how it goes...

If you just want to change the text in the existing header row, there's no
need to create a new row...
 
Back
Top