【问题标题】:CellMouseDoubleClick fires multiple times on DataGridView c#CellMouseDoubleClick 在 DataGridView c# 上多次触发
【发布时间】:2017-03-26 22:44:52
【问题描述】:

我在 c# 中有 WinForms 应用程序。

我想在动态创建的 DataGridView 上添加事件。 我想在双击时添加的操作是删除单击的行。

问题是事件会触发多次,每个行索引都高于被单击的行。

例如,我的 DataGridView 中有 5 个项目,然后单击第二个。 事件触发 4 次,删除第 2、3、4、5 个元素。

如果我点击最后一个,它可以正常工作(因为他是更高的索引)。

任何想法......

【问题讨论】:

标签: c# winforms datagridview


【解决方案1】:

这是一个例子,我留了一些空间,以便在您需要时删除一天中的多个选定行

 if (e.Button == MouseButtons.Left)
    {
        var hti = dataGridView1.HitTest(e.X, e.Y);
        if (hti.RowIndex != -1)
        {
            dataGridView1.ClearSelection();
            dataGridView1.Rows[hti.RowIndex].Selected = true;
        }
    }

    foreach (DataGridViewRow row in DataGridView1.SelectedRows)
    {
        DataGridView1.Rows.Remove(row);
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多