How can i make GridView Header dynamic?

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

coolCoder

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
 
M

Mark Rae [MVP]

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...?
 
R

rote

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

Mark Rae [MVP]

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";
}
}
 
R

rote

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

rote

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";
}
}
 
M

Mark Rae [MVP]

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,266
Messages
2,571,087
Members
48,773
Latest member
Kaybee

Latest Threads

Top