0

我有一个 BindingList 作为数据源和一个网格控件,如果用户删除网格中的一行,我希望他得到确认(例如消息框),

我正在使用的网格控件(我猜它们中的大多数),调用具有 void 返回值的 Collection 的 RemoveAt(int index) 方法,即使我将从 bindingList 继承,覆盖该方法或提供一个新的对于它(和其他)的实现,它无济于事,因为网格无法知道用户选择取消操作......

有没有办法只使用数据绑定和库存绑定列表、网格控件来解决这个问题?

我所做的解决方法是:继承表单BindingList,实现ICancellableRemoving(其中包含单个“bool RemoveItem(object item)”。b.从网格继承,覆盖它的remove方法(或类似),检查数据源是否实现了ICancellableRemoving,如果是,调用该方法,检查结果并相应地取消/继续操作。

PS我实现了一个接口,因为BindingList中唯一的“Remove *”方法,它有一个bool返回值是Collection的Remove(T Item),它是通用的......网格不是;)

在此先感谢,埃里克。

4

2 回答 2

1

附加到OnRowDeleting()

于 2009-11-11T22:10:39.957 回答
0

这是 WinForms 吗?如果是这样,则 WinForms 中的 DataGridView 控件具有您可以调用的 UserDeletingRow() 事件。例如:

// 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;
}
于 2009-11-11T22:21:02.260 回答