【问题标题】:how to disable datagridview mouse click event for headers in c#如何在c#中禁用标题的datagridview鼠标单击事件
【发布时间】:2014-04-25 20:20:49
【问题描述】:

我正在尝试在鼠标单击事件中从 datagridview 中获取价值。

在我单击 datagridview 中的标题之前效果很好。我收到一条错误消息:

Index was out of range. Must be non-negative and less than the size of the collection. Parameter name:index

我使用这段代码来检测鼠标点击:

 private void dataGridView1_CellMouseClick(Object sender, DataGridViewCellMouseEventArgs e)
        {
            if (dataGridView1.Rows[e.RowIndex].Index != -1)
            {
                textBox1.Text = dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString();
            }
            else
            {
                textBox1.Text = "";
            }
        }

"dataGridView1.Rows[e.RowIndex].Index != -1" 不工作,我不知道为什么。

任何在标题上禁用鼠标单击事件的简单解决方案都会很棒!谢谢!

我也用:

dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;

选择整列并且 CellContentClick 不起作用。如果幸运的话,CEllContentClick 可以工作 3/10 次。

【问题讨论】:

    标签: c# datagridview click mouse


    【解决方案1】:

    试着改写这个:

    private void dataGridView1_CellMouseClick(Object sender, DataGridViewCellMouseEventArgs e)
    {
        if (e.RowIndex != -1)
        {
            textBox1.Text = dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString();
        }
        else
        {
            textBox1.Text = "";
        }
    }
    

    【讨论】:

    • 谢谢。我不得不将 if 语句更改为:if (e.ColumnIndex != -1 && e.RowIndex != -1),因为我还使用 dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;不仅仅是 RowIndex 问题,还有 ColumnIndex 问题 :) 再次感谢!
    • @MatejMerc - 所以,我的解决方案对你有帮助吗?
    猜你喜欢
    • 1970-01-01
    • 2012-11-28
    • 1970-01-01
    • 2011-05-30
    • 2014-05-29
    • 2022-01-12
    • 2017-03-09
    • 2016-05-02
    • 1970-01-01
    相关资源
    最近更新 更多