August 14, 2004

Add 2nd Line to Grid Item



I'm trying to add a custom row for each Item and Alternating 
Item that is added to my datagrid.  The idea is for each row created, 
to create another row beneath it, so I can have one cell that spans 
all the columns to display a lot of text.


Heres the code i'm using for the OnItemCreated event:


private void dgAlerts_ItemCreated(object sender,

System.Web.UI.WebControls.DataGridItemEventArgs e)
{
DataGridItem item = e.Item;
// if its not an item or alternating item, get out of here
if (item.ItemType != ListItemType.Item & item.ItemType !=
ListItemType.AlternatingItem)
return;
// we need to access the datarow to get the text, so lets get it
DataRow dataRow = ((DataRowView) item.DataItem).Row;
//now we need to create the new row, add the cell and put in some text.

DataGridItem oNewItem = new

DataGridItem(item.ItemIndex,item.DataSetIndex,item.ItemType);

TableCell oNewCell = new TableCell();
oNewItem.Cells.Add( oNewCell);
oNewCell.ColumnSpan = item.Cells.Count;
Label oFullText = new Label();
oFullText.Text = (string)
dataRow[(int)enumAlertDataGridColumns.Details];
oNewCell.Controls.Add(oFullText);

// now w need to add it to the table (is always first item in
datagrid controls) and we're done!

dgAlerts.Controls[0].Controls.Add(oNewItem);

}

If you change the event from ItemCreated to ItemDataBound
and use the same code, you will get the custom row below instead of
above your items.

<asp:DataGrid id="dgAlerts" OnItemDataBound="dgAlerts_ItemDataBound" .... />



and change the name of the private void, to dgAlerts_
ItemDataBound instead of ItemCreated.The created event has
not created your item yet, so when you append to the Controls[0]
you get it before the DataBound one. So when you change
it to the DataBound one, the Item is already created and you can
then append the new row AFTER the DataBound one.


Posted by sachauncey at August 14, 2004 03:02 PM