如何禁用在 DataGridView 中选择的能力? [英] How to disable the ability to select in a DataGridView?
问题描述
我只想使用我的 DataGridView
来显示内容,并且我希望用户无法从 DataGridView
中选择任何行、字段或任何内容.
I want to use my DataGridView
only to show things, and I want the user not to be able to select any row, field or anything from the DataGridView
.
我该怎么做?
推荐答案
我会这样做:
private void myDataGridView_SelectionChanged(Object sender, EventArgs e)
{
dgvSomeDataGridView.ClearSelection();
}
我不同意没有 DataGridView
应该是不可选择的广泛断言.一些 UI 是为工具或触摸屏构建的,允许选择会误导用户认为选择实际上会将它们带到某个地方.
I don't agree with the broad assertion that no DataGridView
should be unselectable. Some UIs are built for tools or touchsreens, and allowing a selection misleads the user to think that selecting will actually get them somewhere.
在控件上设置ReadOnly = true
对是否可以选择单元格或行没有影响.设置 Enabled = false
存在视觉和功能上的缺点.
Setting ReadOnly = true
on the control has no impact on whether a cell or row can be selected. And there are visual and functional downsides to setting Enabled = false
.
另一种选择是将控件选择的颜色设置为与未选择的颜色完全相同,但如果您碰巧操纵了单元格的背景颜色,那么这种方法也会产生一些令人讨厌的结果.
Another option is to set the control selected colors to be exactly what the non-selected colors are, but if you happen to be manipulating the back color of the cell, then this method yields some nasty results as well.
这篇关于如何禁用在 DataGridView 中选择的能力?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!