Accessing data object on delete command.

C

Cappy

Hi guys.

I have a datagrind that is bound to a custom collection of a custom
object.

When a delete button is pressed in the datagrid, I not only want to
remove the row from the grid, I want to delete the object from the
collection that is bound to the grid.

So far I have this.

public void groupsGrid_DeleteCommand(object sender,
DataGridCommandEventArgs e)
{


try
{
//My Group object UMGroup grouptodelete =
(UMGroup)e.Item.DataItem;

// attempt delete
groupsCollection.Remove(grouptodelete);

}

catch(Exception exp)
{
Errortxt.Text = exp.Message;
}

//Disable Edit mode
groupsGrid.EditItemIndex = -1;

//Refresh data in grid
PopulateDatagrid();
}

My problem is, grouptodelete comesback with object undefined. Is there
away of getting access to the object that is supplying data to the row
that fired the delete command?

Thanks
Amit
 
E

Elton W

Hi Cappy,

In order to delete the object, you should get it first.
Suppose you have a key for the custom object. You can get
the key in couple of ways:

string key = datagrid.DataKeys[e.Item.ItemIndex];
string key = e.Item.Cells[key_Column_Index].Text; // for
BoundColumn
string key = ((contrl_type)e.Item.FindControl
("key_Column_ID")).Text; // for TemplateColumn

Then you can get your custom object by the key:
UMGroup grouptodelete = method_of_find_object(key);
// then delete the object
if (grouptodelete != null)
{
groupsCollection.Remove(grouptodelete);

}

HTH


Elton Wang
(e-mail address removed)

-----Original Message-----
Hi guys.

I have a datagrind that is bound to a custom collection of a custom
object.

When a delete button is pressed in the datagrid, I not only want to
remove the row from the grid, I want to delete the object from the
collection that is bound to the grid.

So far I have this.

public void groupsGrid_DeleteCommand(object sender,
DataGridCommandEventArgs e)
{


try
{
//My Group object
UMGroup
grouptodelete =
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top