Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 4010
  • Last Modified:

Gridview - Dynamic Header Text

I have an exisiting gridview (named Gridview1) that I dynamically add a column to.

At the moment  the code below works fine but what do I need to add in order for it to return dynamic header text instead of the text "Label1Header"?
(eg. how do I return the dynamic header text  as the defined column data result from an objectdatasource?).

Thanks!



Current Code:-

protected void Page_Load {object sender, EventArgs e}
{
if (!IsPostBack)
BuildColumnsDynamically();
}

private void BuildColumnsDynamically()

{
BoundField nameColumn = new BoundField ();
nameColumn. DataField = "Label1";
NameColumn.HeaderText = "Label1Header"

GridView1.Columns.Add(nameColumn);
}

}







0
doolinn
Asked:
doolinn
  • 2
1 Solution
 
bthewsCommented:
"eg. how do I return the dynamic header text  as the defined column data result from an objectdatasource?"

When you says this, do you mean you want to name the column based on the data you get back from this column?
So you are not always going to know what kind of data is coming back from this column?

As a simple example, lets say that there are the names of states in this column - you don't want to name the column "States", but instead, you want to name it "California" or something like that?

Just trying to determine what you are asking for.

If you are tyring to name the header based on the data, I think you'll have to use the ItemDataBound method (for c#) this event gets fired when data is populated into the grid
0
 
doolinnAuthor Commented:

Just to clarify ..to expand on your "States" example ..I intend to show the word "states" in different languages in the header based on a drop down language  box.

ie when English is selected, "States" is shown as the header title, when French is selected the word "√Čtats" is in the header etc etc..

I have a translation table in my database that is being queried via an objectdatasource...it returns one record for each language..I'm comforable with that part of the application...I'm just unsure how to link that result back to the gridview header...

hope this clarifies things!

Thanks again for your help
0
 
doolinnAuthor Commented:
Its OK..I've worked out that I should be using a headertemplate - works fine now.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now