如何防止datagridview为每个选定的行触发userdeletingrow事件



我如何阻止DataGridView控件触发UserDeletingRow事件为每一个选定的行后,我已经消耗了第一个?

这个网格被绑定到我的wcf-webservice中的对象,我想对所有应该被删除的对象只执行一次delete方法。

这个事件处理程序被触发为每一个选定的行,也总是触发"你真的想删除"-messageboxes:

private void Grid_UserDeletingRow(object sender, DataGridViewRowCancelEventArgs e)
{
    switch (this.Type)
    {
        case AdminType.Channel:
            List<Channel> channels = GrdChannel.SelectedRows.Cast<DataGridViewRow>()
              .Select(row => (Channel)row.DataBoundItem).ToList();
            e.Cancel = !Delete_Channels(channels);
            break;
        // other types ...
        default:
            break;
    }
}

这个方法在确认后调用webservice:

private bool Delete_Channels(List<Channel> channels)
{
    var msg = string.Format("Do you really want to delete {0}?", channels.Count == 1 ? "this channel" : "these channels");
    var title = channels.Count == 1 ? "Delete channel" : "Delete channels";
    bool yes = MessageBox.Show(msg, title, MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.Yes;
    if (yes)
    {
        using (var db = new ERP_ServiceClient())
            db.DeleteChannels(channels, this.IdUser);
        string message = string.Format("{0} deleted successfully: {1}"
                                      , channels.Count == 1 ? "Channel" : "Channels"
                                      , string.Join(",", channels.Select(p => p.Name)));
        channelBindingSource.Remove(channels);
        Main.ShowStatusMessage(message);
    }
    return yes;
}

好的,显然没有更好的方法(在可读性方面),所以我实现了Tigrans的建议,在事件处理程序中使用计数变量,并仅在最后选择的行触发此事件后开始删除。

下面是解决方法:

private int _deleteCount = 0;
private bool _deleting = false;
private bool _reallyDelete = false;
private IEnumerable<object> _deleteEntities = null;
private void Grid_UserDeletingRow(object sender, DataGridViewRowCancelEventArgs e)
{
    DataGridView grid = (DataGridView)sender;
    if (!_deleting)
    {
        _deleting = true;
        _deleteCount = grid.SelectedRows.Count;
        _deleteEntities = grid.SelectedRows.Cast<DataGridViewRow>().Select(r => r.DataBoundItem).ToList();
        string msg = "";
        string title = "";
        switch (this.Type)
        {
            case AdminType.Channel:
                msg = string.Format("Do you really want to delete {0}?", _deleteCount == 1 ? "this channel" : "these channels");
                title = _deleteCount == 1 ? "Delete channel" : "Delete channels";
                break;
            // other types ...
            default:
                break;
        }
        _reallyDelete = MessageBox.Show(msg, title, MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.Yes;
    }
    e.Cancel = !_reallyDelete;
    // wait until all events are triggered before starting to delete
    if (--_deleteCount == 0)
    {
        switch (this.Type)
        {
            case AdminType.Channel:
                List<Channel> channels = _deleteEntities.Cast<Channel>().ToList();
                Delete_Channels(channels);
                break;
            // other types ...
            default:
                break;
        }
        _deleting = false;
        _reallyDelete = false;
        _deleteEntities = null;
    }
}

将UserDeletingRow事件替换为key_Down事件

if (e.KeyValue == 46)//删除Key

{处理= MessageBox。Show("Do you want really to delete selected rows")"确认",MessageBoxButtons。OKCancel MessageBoxIcon。=对话sult. ok;}

相关内容

  • 没有找到相关文章

最新更新