Programmatically adding attributes to DataGrid columns

  • Thread starter Thread starter Mike Irwin
  • Start date Start date
M

Mike Irwin

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/d...genref/html/cpcondatagridwebservercontrol.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?
 
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;
intCell.Attributes.Add("onmouseover", "this.bgColor='Silver'");
intCell.Attributes.Add("onmouseout", "this.bgColor='Gainsboro'");
}
}
}
 
Back
Top