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