all groups > asp.net datagrid control > october 2003 >
You're in the

asp.net datagrid control

group:

ItemCreate event handler changes wrong DataGridItem


ItemCreate event handler changes wrong DataGridItem Chris Smith
10/30/2003 2:25:16 PM
asp.net datagrid control:
I am changing the EditText in a EditCommandColumn in a DataGrid in an
ItemCreate event handler . The only problem I am having is that instead of
changing the text in the current row it changes the next row.

I watch the value in the debugger, and I see it change at what appears to be
the right time.

The EnableViewState is set to True.

Any suggestions or thoughts appreciated!



I read a value out of the current DataGridItem DataItem (a person's name),
and use it to replace the EditCommandColumn's EditText field.:

private void hhItemCreated(object sender,
System.Web.UI.WebControls.DataGridItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType ==
ListItemType.AlternatingItem)
{
string HouseholdName;

DataGrid dg;
dg = (DataGrid)sender;

EditCommandColumn ecc;
ecc = (EditCommandColumn)dg.Columns[0];

Troop.FunWithXML.TroopDB.HouseholdRow r =
(Troop.FunWithXML.TroopDB.HouseholdRow)e.Item.DataItem;
if(r == null) return;

HouseholdName =
((Troop.FunWithXML.TroopDB.HouseholdRow)e.Item.DataItem)["HHName"].ToString(
);
ecc.EditText = HouseholdName;
}
}

Re: ItemCreate event handler changes wrong DataGridItem Giorgio Parmeggiani
11/1/2003 5:36:45 PM
Hi

[quoted text, click to view]

I think your example doesn't work because at the ItemCreated event the page
has already created the LinkButton of the column using the "EditText"
property of the EditCommandColumn.

Therefore you must modify directly the "Text" property of the LinkButton.

here an example:

private void hhItemCreated(object sender,
System.Web.UI.WebControls.DataGridItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType ==
ListItemType.AlternatingItem)
{
string HouseholdName;

Troop.FunWithXML.TroopDB.HouseholdRow r =
(Troop.FunWithXML.TroopDB.HouseholdRow)e.Item.DataItem;
if(r == null) return;

HouseholdName =
((Troop.FunWithXML.TroopDB.HouseholdRow)e.Item.DataItem)["HHName"].ToString(
);
((LinkButton)e.Item.Cells[0].Controls[0]).Text = HouseholdName;

}


Hi
Giorgio

AddThis Social Bookmark Button