Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

assigning datagrid.headertext programmatically

Posted on 2005-03-30
11
Medium Priority
?
810 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
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

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.

Question has a verified solution.

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

This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
Sometimes in DotNetNuke module development you want to swap controls within the same module definition.  In doing this DNN (somewhat annoyingly) swaps the Skin and Container definitions to the default admin selections.  To get around this you need t…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses
Course of the Month10 days, 18 hours left to enroll

572 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