Datagrid header text

B

Ben

Hi, I'm wondering how can i change the datagrid header text at runtime so
that it keeps the sorting enabled?

I can do;

e.Item.Cells[0].Text = "foo";

But then foo is the text of the table cell and not the underlying hyperlink
and as such, foo is no longer sortable.

I've tried things like...

((HyperLink)(e.Item.Cells[0].Controls[0]).Text = "foo";

But I get a null exception. In fact for some unknown reason,
e.Item.Cells[0].Controls.Count == 0!!!!

Can someone please show me what it is that I'm doing wrong here?

Thanks,
Ben
 
S

S. Justin Gengo

Ben,

I think you'll have to use "FindControl" and then cast the control found to
the hyperlink.

--
Sincerely,

S. Justin Gengo, MCP
Web Developer / Programmer

www.aboutfortunate.com

"Out of chaos comes order."
Nietzsche
 
S

Steven Cheng[MSFT]

Hi Ben,

From your description, you're wondering how to change the DataGrid(has set
columns's sort property) 's header text at runtime and also keep the
datagrid sortable ,yes?

Based on my research, the Header in the DataGrid in the Asp.net DataGrid
control is represented by a DataGridLinkButton control(is a internal
control hidden from developer) rather than a normal HyperLink or LinkButton
control. So we can retrieve it as generally we do. However, I think we can
change the Header's Text by setting the certain DataGrid Column's
HeaderText property since the HeaderText is set for each Column, isn't it?
For example:

dgMain.Columns[1].HeaderText = "fdsaf";

Thus, we can get the proper columns via DataGrid.Columns collection in a
certain postback event and then change its HeaderText , but be care that we
have to rebind the DataGrid with datasource after we change the HeaderText
of a column , for example:

private void btnChangeSortText_Click(object sender, System.EventArgs e)
{
dgMain.Columns[1].HeaderText = "fdsaf";
Bind_Data();
}

Hope helps. Thanks.


Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Get Preview at ASP.NET whidbey
http://msdn.microsoft.com/asp.net/whidbey/default.aspx
 
B

Ben

After doing some research it looks like a good place to do this is in the
ItemCreated event of the datagrid. The one catch is that the itemcreated
fires in the viewstate init function. Which I thought was odd.

Nevertheless, here is what I ended up doing...


private void dgSubordinate_ItemCreated(object sender,
System.Web.UI.WebControls.DataGridItemEventArgs e)
{
if (e.Item.ItemType != ListItemType.Header) return;

string HeaderText = null;

if (oSecurity != null)
{
switch (oSecurity.ReportType)
{
case SecurityPermissions.Regional:
HeaderText = "SVP";
break;
case SecurityPermissions.SVP:
HeaderText = "VP";
break;
case SecurityPermissions.VP:
HeaderText = "AE";
break;
}
((LinkButton)e.Item.Cells[0].Controls[0]).Text = HeaderText;
}
}


So, that's working just fine. I ended up needing the oSecurity != null
check becuase of the fact that the ItemCreate is fired in the
viewstateinit(or whatever its called). Since my security object doesn't
exist until Page_Load, I needed the null check. At some point I'll look
into the initilization of the datagrid and see why it's creating items so
early, but I can guess. Anyway, thanks for your help.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top