views:

101

answers:

2

Hi, i have a BindingList as a datasource and a grid control, if the user deletes a row in the grid, i want him to get a confirmation (a messagebox for example),

the grid control i'm using (and i guess most of them), call the RemoveAt(int index), method of Collection which has a void return value, even if i'll inherit from bindingList, override the method or provide a new implemtation for it (and other), it won't help because the grid has no way to know that the user chose to cancel the operation...

is there a way around the problem using only databinding and the stock bindinglist, grid control?

the workaround i did is : a. inherit form BindingList, implement ICancellableRemoving (which contains a single "bool RemoveItem(object item)". b. inherit from the grid,override it's remove method (or similar), check if the datasource implements ICancellableRemoving, if it does, call the method, examine the result and cancel/proceed with the operation accordingly.

P.S I implemented an interface because the only "Remove*", method in BindingList, which has a bool return value is Remove(T Item) of Collection, and it's generic... the grid isn't ;)

Thanks in advance, Erik.

+1  A: 

attach to OnRowDeleting()

Chris Ballance
thanks for the quick reply, but i think it's worse than my solution...this logic should be implemented by the list...putting the logic in this method means that i'll need to override similar mothods in every control that deletes rows and wants the users confirmation.. which means duplication and bad seperation of concerns...
Erik Ashepa
Actually it doesn't repeat code. You can write one event handler and then wire the event to every grid's Deleting event. It's not really any different than your workaround above except it's a lot easier.
Scott Anderson
in terms of duplication it pretty much like my solution i guess, but the problem i'm describing is that grids have no way to know if the item is removed from the datasource... they always assume it is... (void return type) so they always remove the row from the grid...is there a solution to this problem? maybe i'm missing something?
Erik Ashepa
actually, the event handler's singature may not match every grids "removeitem" method.. then my solution is a bit more generic.. but that really doesn't matter.. ;)
Erik Ashepa
If you wire up this event and then check the user's response to your MessageBox and subsequently set "e.Cancel = true", then the grid will never delete the row. If the user responds "yes" to your dialog then the bool "Cancel" never gets set, and the datagrid will handle removing the row from the UI and your bindinglist at the same time.
Scott Anderson
Are you using different types of grid controls? If that's the case then your solution is probably the way to go. If they're all the same type, then I don't see why you couldn't use one static event handler in a helper class somewhere and just wire up the grids you care about to that handler.
Scott Anderson
Thank you for your answer! :)Just wanted to see if there is a way to achieve this with databinding... guess not.. BTW, this is true for ObservableCollection<T> as well :)
Erik Ashepa
A: 

Is this WinForms? If so, the DataGridView control in WinForms has a UserDeletingRow() event that you can call. For example:

// wire up the event
myGrid.UserDeletingRow += new DataGridViewRowCancelEventHandler(myGrid_UserDeletingRow);

// event handler
private void myGrid_UserDeletingRow(object sender, DataGridViewRowCancelEventArgs e)
{
    DialogResult result = MessageBox.Show("Are you sure you wish to delete this row?", "Are you sure?", MessageBoxButtons.YesNo, MessageBoxIcon.Question);

    if (result == DialogResult.No)
        e.Cancel = true;
}
Scott Anderson