当行标题单击选择了datagridview行时,如何阻止输入单元格? [英] How can I stop cells from being entered when a datagridview row is selected by a row header click?

查看:49
本文介绍了当行标题单击选择了datagridview行时,如何阻止输入单元格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每当用户单击行标题时,它将选择整行(并突出显示为蓝色),则实际上会输入第一列中的单元格.也就是说,如果您开始输入东西,则文本将进入该单元格.我想防止这种情况.选择一行或多行时,是否可以不输入datagridview单元格?

Whenever a user clicks the row header, which selects the whole row (and highlights it blue), the cell in the first column is actually entered. That is, if you start typing stuff, the text goes in that cell. I want to prevent this. Would it be possible to have no datagridview cells being entered when one or many rows are selected?

我还需要一种解决方案,以防止在单击和拖动行标题引起的多行选择期间输入单元格.

I also need the solution to prevent cells being entered during multiple row selections caused by clicking and dragging on the row headers.

关于如何实现这一目标的任何想法?

Any ideas on how I could achieve this?

谢谢

以撒

推荐答案

将DataGridView的ReadOnly属性设置为true或false,这取决于是否选择了一行或多行,或者DGV的"CellState"是否已更改.

Set your DataGridView's ReadOnly property to true or false, depending on whether one or more rows have been selected or if the DGV's 'CellState' has changed.

为您的DataGridView添加以下两个事件:

Add the following two events for your DataGridView:

private void dataGridView1_RowStateChanged(object sender, DataGridViewRowStateChangedEventArgs e) {
    if (e.StateChanged == DataGridViewElementStates.Selected) {
        Console.WriteLine("TRUE");
        dataGridView1.ReadOnly = true;
    }
}

private void dataGridView1_CellStateChanged(object sender, DataGridViewCellStateChangedEventArgs e) {
    if (e.StateChanged == DataGridViewElementStates.Selected) {
        Console.WriteLine("false");
        dataGridView1.ReadOnly = false;
    }
}

这在我的测试中对我有用,但是如果隐藏了陷阱",我也不会感到惊讶.

This worked for me in my tests but I wouldn't be surprised if there were hidden 'gotchas.'

这篇关于当行标题单击选择了datagridview行时,如何阻止输入单元格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆