C# DataGridView:如何选择整个列并取消选择其他所有内容?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1987193/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
DataGridView: How to select an entire Column and deselect everything else?
提问by Y_Y
I've been trying to find out how to select all cells under a Column with a 'mouse right click+menu+Select this Column'...
我一直在尝试找出如何使用“鼠标右键单击+菜单+选择此列”来选择列下的所有单元格...
MSDN isn't helping much...
MSDN 没有多大帮助...
I get this error when I try to change selection mode:
当我尝试更改选择模式时出现此错误:
DataGridView control's SelectionMode cannot be set to FullColumnSelect while it has a column with SortMode set to DataGridViewColumnSortMode.Automatic.
Thanks, Y_Y
谢谢,Y_Y
采纳答案by SLaks
Loop through the cells in the column and set their Selected property to true.
It sounds horrible, but I believe it's the only way to select an entire column and keep automatic sorting.
遍历列中的单元格并将它们的 Selected 属性设置为 true。
这听起来很可怕,但我相信这是选择整列并保持自动排序的唯一方法。
For example:
例如:
grid.ClearSelection();
for(int r = 0; r < grid.RowCount; r++)
grid[columnIndex, r].Selected = true;
回答by David
Sorry it took so long - I wanted to test before I answered, so I plopped this into Visual Studio to test first.
抱歉,花了这么长时间 - 我想在回答之前先测试一下,所以我先把它放到 Visual Studio 中进行测试。
I had to do this in mine to get it to work:
我必须在我的中执行此操作才能使其正常工作:
foreach (DataGridViewColumn c in dataGridView1.Columns)
{
c.SortMode = DataGridViewColumnSortMode.NotSortable;
c.Selected = false;
}
dataGridView1.SelectionMode = DataGridViewSelectionMode.FullColumnSelect;
dataGridView1.Columns[0].Selected = true;
回答by Tanner Ornelas
You need 3 things.
你需要三样东西。
- Clear all selected rows and cells.
- Remove the sort mode of every column to Not sortable. The default click event is sort, now it will be select.
- Set the selection mode to column.
- 清除所有选定的行和单元格。
- 将每列的排序模式删除为不可排序。默认的点击事件是排序,现在它将是选择。
- 将选择模式设置为列。
Finally you can select the first column to show user the selection mode. This only have to be done once. The first time you load your form or your datagridview.
最后,您可以选择第一列向用户显示选择模式。这只需要做一次。第一次加载表单或 datagridview 时。
// Clear all selected cells or rows in the DGV.
dataGridView1.ClearSelection();
// Make every column not sortable.
for (int i=0; i < dataGridView1.Columns.Count; i++)
dataGridView1.Columns[i].SortMode = DataGridViewColumnSortMode.NotSortable;
// Set selection mode to Column.
dataGridView1.SelectionMode = DataGridViewSelectionMode.FullColumnSelect;
// In case you want the first column selected.
if (dataGridView1.Columns.Count > 0 ) // Check if you have at least one column.
dataGridView1.Columns[0].Selected = true;
回答by Vishnu Raja
I got this error while starting with WPF using the drag and drop interface and none of the manual coding. Viewing the properties of datagrid would give a way to select items like this:
我在使用拖放界面和没有手动编码的 WPF 开始时遇到了这个错误。查看 datagrid 的属性将提供一种选择项目的方法,如下所示:
But trying to change to type to Column Header Select or Column Select would result in the error you mentioned.
但是尝试更改为 Column Header Select 或 Column Select 类型会导致您提到的错误。
So how it was solved was by right-clicking on the grid and go to Edit Columns. Here all the columns and their SortingMode is available to change. Change them all to NotSortable.
所以它的解决方法是右键单击网格并转到Edit Columns。此处所有列及其 SortingMode 均可更改。将它们全部更改为 NotSortable。