By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,152 Members | 1,062 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,152 IT Pros & Developers. It's quick & easy.

Programmatically adding attributes to DataGrid columns

P: n/a
I'm trying to add an attribute to each column header of a DataGrid. I know
that I need to do this within the ItemCreated event of the DataGrid, but I'm
stuck on how to go any further.

I found some info on MSDN on how to do this, but it's doesn't clear any
thing up for me Here's the paragraph and a link to the document:

http://msdn.microsoft.com/library/de...vercontrol.asp

"To add an attribute to the <td> tag, first get the TableCell object that
represents the cell in the DataGrid control to which you want to add the
attribute. The Control.Controls collection for the Item property
(DataGridItemEventArgs indexer) of the DataGridItemEventArgs passed into the
event handler can be used to get the desired TableCell. You can then use the
AttributeCollection.Add method of the Attributes collection for the
TableCell to add attributes to the <td> tag."

Can anyone point me in the right direction?
Nov 15 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Nevermind this question, I figured it out. Here's how I did it in case
anyone wants to know later:

private void DataGrid1_ItemCreated(object sender,
System.Web.UI.WebControls.DataGridItemEventArgs e)
{
ListItemType itemType = (ListItemType)e.Item.ItemType;
if (itemType == ListItemType.Header)
{
// There's 5 columns in DataGrid1
for (int i = 0; i <= 4; i++)
{
TableCell intCell = (TableCell)e.Item.Controls[i];
intCell.Attributes.Add("onmouseover", "this.bgColor='Silver'");
intCell.Attributes.Add("onmouseout", "this.bgColor='Gainsboro'");
}
}
}

"Mike Irwin" <mi**@faroutfreakyshit.com> wrote in message
news:uL*************@TK2MSFTNGP12.phx.gbl...
I'm trying to add an attribute to each column header of a DataGrid. I know that I need to do this within the ItemCreated event of the DataGrid, but I'm stuck on how to go any further.

I found some info on MSDN on how to do this, but it's doesn't clear any
thing up for me Here's the paragraph and a link to the document:

http://msdn.microsoft.com/library/de...vercontrol.asp
"To add an attribute to the <td> tag, first get the TableCell object that
represents the cell in the DataGrid control to which you want to add the
attribute. The Control.Controls collection for the Item property
(DataGridItemEventArgs indexer) of the DataGridItemEventArgs passed into the event handler can be used to get the desired TableCell. You can then use the AttributeCollection.Add method of the Attributes collection for the
TableCell to add attributes to the <td> tag."

Can anyone point me in the right direction?

Nov 15 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.