开发者

.NET DataBinding, Confirmation before removing items from the list

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.


attach to OnRowDeleting()


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;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜