?
Solved

assigning datagrid.headertext programmatically

Posted on 2005-03-30
11
Medium Priority
?
799 Views
Last Modified: 2008-02-01
Hi!
I try to change the headertext of a datagrid programmatically from my codebehind. How can I go about that? I am using VB.net, and my public string variable shows up on the page when placed just into the space there:
<%=HeaderText%>
but when I put it into the headertemplate oder templatecolumn (property: headertext) nothing shows up. Any ideas?
0
Comment
Question by:ddetering
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
11 Comments
 
LVL 11

Expert Comment

by:sachiek
ID: 13669131
A simple alternate way is to place label on top of datagrid.
You can change it's text anytime without any problem

label.Text = "";


Sachi
0
 
LVL 2

Expert Comment

by:neil_richards
ID: 13669150
You could also try changing the header while performing your databind.  Sorry I don't have it in VB.NET.

private void Page_Load(object sender, System.EventArgs e)
{
      if (!IsPostBack)
      {
            ArrayList al = new ArrayList();
            al.Add("Ringo");
            al.Add("Paul");
            al.Add("John");

            DataGrid1.DataSource = al;
            DataGrid1.DataBind();
      }
}

private void InitializeComponent()
{    
      this.DataGrid1.ItemDataBound += new System.Web.UI.WebControls.DataGridItemEventHandler(this.DataGrid1_ItemBinding);
      this.Load += new System.EventHandler(this.Page_Load);

}

private void DataGrid1_ItemBinding(Object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
      if (e.Item.ItemType == ListItemType.Header )
      {
            e.Item.Cells[0].Text = "I like traffic lights";
      }
}
0
 
LVL 6

Expert Comment

by:apb2
ID: 13670654
Inside a datagrid use

<%# HeaderText%>

Instead of

<%=HeaderText%>


apb2
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 1

Author Comment

by:ddetering
ID: 13672511
Well, I am sorry, but none worked so far, even though I had not tried Neil's suggestion. (Guess I am too lazy?)

Anyway, apb2's doesn't work because # is for databinding, but I am not doing that. Sachiek's, I guess, would work in a way but would deprive me of sorting etc.

I ended up replacing my template column with a normal databound column which you can manipulate fully from code behind, like this:
datagrid.Columns(3).HeaderText = "This comes from behind!"

Now, who should get the points?
0
 
LVL 2

Expert Comment

by:neil_richards
ID: 13672752
I still think you should give my way a shot.  Seriously, all you need to do is convert it to vb.net.
0
 
LVL 1

Author Comment

by:ddetering
ID: 13673253
doesn't your way require boundcolumns instead of templatecolumns, just like mine?
0
 
LVL 2

Accepted Solution

by:
neil_richards earned 500 total points
ID: 13673520
bleh, you're right.  I can't see anyway to do it with a template column.  :(
0
 
LVL 1

Author Comment

by:ddetering
ID: 13673622
You are getting the points because your way at least works some way without giving up functionalibility.
Cheers!
0
 
LVL 2

Expert Comment

by:neil_richards
ID: 13674021
I think I've figured it out with the TemplateColumn:

in your aspx page:

<Columns>
      <asp:TemplateColumn HeaderText="MyText">
            <ItemTemplate><%# DataBinder.Eval(Container, "DataItem")%></ItemTemplate>
      </asp:TemplateColumn>
</Columns>

and in your aspx.cs (or .vb)

if (!IsPostBack)
{
      ArrayList al = new ArrayList();
      al.Add("Ringo");
      al.Add("Paul");
      al.Add("John");

      DataSet ds = new DataSet();
      DataGrid1.DataSource = al;
      DataGrid1.DataBind();
      
      TemplateColumn tc = (TemplateColumn)DataGrid1.Columns[0];
      tc.HeaderText = "My Bad";
      tc.Visible = true;
      DataGrid1.DataBind();
}

It's worked on my PC with a template column.  Hopefully it should work on yours.  If you were using a proper dataset it might be a little easier
(ie. <ItemTemplate><%# DataBinder.Eval(Container.DataItem, "ColumnName")%></ItemTemplate>)
0
 
LVL 1

Author Comment

by:ddetering
ID: 13674644
Yes, this works. In VB.net, it is
Dim tc As TemplateColumn = dgrdWhatEver.Columns(2)
tc.HeaderText = "new header text"

Haven't figured it out how to work with <headertemplate> yet where I actually would need to assign the text to a linkbutton, so I am running into the same layout problem as with my previous solution: The clickable (to sort) headertext doesn't look like all the other links because it follows the (default) headerstyle unless I manually design it.

Anyway, good work! (Or do you happen to know how to accomplish this with the text of a linkbutton in a headertemplate?)

Cheers!
0
 
LVL 2

Expert Comment

by:neil_richards
ID: 13676077
Changing the text of a linkbutton is (I think) much more difficult.  After adding a linkButton to my HeaderTemplate I tried doing:

TemplateColumn tc = (TemplateColumn)DataGrid1.Columns[0];
tc.HeaderText = "My Bad";
tc.Visible = true;

Control c = new Control();
tc.HeaderTemplate.InstantiateIn(c);
((LinkButton)c.Controls[1]).Text="Changing Dynamically";
DataGrid1.DataBind();

But it didn't seem to reflect the change, even though in debugging c.Controls[i] is clearly a link button.  I'm going to have a think about this but I don't believe the solution will be easy.

You could always loop over the DataGrid, reading all the controls and their child controls (recursively), but that seems very inefficient.  
0

Featured Post

Independent Software Vendors: 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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Suggested Courses

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question