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?
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?
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.ItemDataBou nd += new System.Web.UI.WebControls. DataGridIt emEventHan dler(this. DataGrid1_ ItemBindin g);
this.Load += new System.EventHandler(this.P age_Load);
}
private void DataGrid1_ItemBinding(Obje ct sender, System.Web.UI.WebControls. DataGridIt emEventArg s e)
{
if (e.Item.ItemType == ListItemType.Header )
{
e.Item.Cells[0].Text = "I like traffic lights";
}
}
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.ItemDataBou
this.Load += new System.EventHandler(this.P
}
private void DataGrid1_ItemBinding(Obje
{
if (e.Item.ItemType == ListItemType.Header )
{
e.Item.Cells[0].Text = "I like traffic lights";
}
}
Inside a datagrid use
<%# HeaderText%>
Instead of
<%=HeaderText%>
apb2
<%# HeaderText%>
Instead of
<%=HeaderText%>
apb2
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).Header Text = "This comes from behind!"
Now, who should get the points?
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).Header
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.
ASKER
doesn't your way require boundcolumns instead of templatecolumns, just like mine?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
You are getting the points because your way at least works some way without giving up functionalibility.
Cheers!
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")%></ItemTemplat e>
</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")%></ItemTempl ate>)
in your aspx page:
<Columns>
<asp:TemplateColumn HeaderText="MyText">
<ItemTemplate><%# DataBinder.Eval(Container,
</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.
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.
ASKER
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!
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.Instanti ateIn(c);
((LinkButton)c.Controls[1] ).Text="Ch anging 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.
TemplateColumn tc = (TemplateColumn)DataGrid1.
tc.HeaderText = "My Bad";
tc.Visible = true;
Control c = new Control();
tc.HeaderTemplate.Instanti
((LinkButton)c.Controls[1]
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.
You can change it's text anytime without any problem
label.Text = "";
Sachi