473,414 Members | 1,630 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,414 software developers and data experts.

Datagrid header text

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

Nov 18 '05 #1
3 2489
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
"Ben" <be*@online.nospam> wrote in message
news:01**************@fe37.usenetserver.com...
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

Nov 18 '05 #2
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

Nov 18 '05 #3
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.
"Steven Cheng[MSFT]" <v-******@online.microsoft.com> wrote in message
news:li**************@cpmsftngxa10.phx.gbl...
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


Nov 18 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Webgour | last post by:
Hi, I'm tring to add a column to a datagrid with a linkbutton as header that can be used to sort the column. The column and the linkbutton are added programmatically (see below). However the...
7
by: Matthew Wieder | last post by:
Hi - I have a datagrid that has a black header and the rows alternate white and gray. The problem is that until items are added to the grid, the grid appears as a large black rectangle, which is...
2
by: rooster575 | last post by:
I would like my datagrid to have the following format: ID - Name - Phone Title - Birthday - email Is this possible for the header and result items? Thanks.
0
by: Danny | last post by:
I am trying to sort my second datagrid. And it works but only after i click 2 times on the column header. The first time i click on the header the data in the right order will add to the rows in...
2
by: saleek | last post by:
I was wondering if there is a way I can add an extra header to a datagrid? I found this solution on the internet - but it seems quite old and didn't work for me. ...
0
by: Daniel Doyle | last post by:
Hello and apologies in advance for the amount of code in this post. I've also sent this message to the Sharepoint group, but thought that ASP.NET developers may also be able to help, even though...
3
by: TPhelps | last post by:
I have a sample of an unbound (autogeneratecolumns is true) sortable/pagable datagrid that works. I want to change one of the columns to a hyperlink. The examples I find use a bound column. I...
2
by: Santosh | last post by:
Dear all i want to bind data to datagrid header template i am wrtting follwing code it is displaying data with in item template not but it is display data in header template <asp:DataGrid...
2
by: =?Utf-8?B?Y3JlYXZlczA2MjI=?= | last post by:
I have a nested datagrid in a xaml file, the parent datagrid loads the vendor information and the details loads the documents for that vendor in a datagrid. Everything is working fine until I click...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.