繁体   English   中英

如何防止datagridview为每个选定行触发UserDeletingRow-event?

[英]How to prevent datagridview from firing UserDeletingRow-event for every selected row?

我已经消耗完第一行后,如何停止DataGridView控件为每个选定行触发UserDeletingRow -event?

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

每个选定的行都会触发此事件处理程序,它还会始终触发“您是否真的要删除”消息框:

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

确认后,此方法调用Web服务:

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建议,以在事件处理程序中使用count变量,并仅在最后选择的行触发此事件后才开始删除。

解决方法如下:

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的rplace事件与此事件key_Down

if(e.KeyValue == 46)//删除密钥

{e.Handled = MessageBox.Show(“您是否真的要删除选定的行”,“ Confirm”,MessageBoxButtons.OKCancel,MessageBoxIcon.Question)!= DialogResult.OK; }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM