Link to home
Start Free TrialLog in
Avatar of ddetering
ddetering

asked on

assigning datagrid.headertext programmatically

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?
Avatar of sachiek
sachiek
Flag of Singapore image

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
Avatar of neil_richards
neil_richards

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";
      }
}
Inside a datagrid use

<%# HeaderText%>

Instead of

<%=HeaderText%>


apb2
Avatar of ddetering

ASKER

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?
I still think you should give my way a shot.  Seriously, all you need to do is convert it to vb.net.
doesn't your way require boundcolumns instead of templatecolumns, just like mine?
ASKER CERTIFIED SOLUTION
Avatar of neil_richards
neil_richards

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
You are getting the points because your way at least works some way without giving up functionalibility.
Cheers!
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>)
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!
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.