DataGridView: How to select an entire Column and deselect everything else?
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...
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 DataGridViewColum开发者_StackOverflow中文版nSortMode.Automatic.
Thanks, Y_Y
Sorry it took so long - I wanted to test before I answered, so I plopped this into Visual Studio to test first.
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;
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.
For example:
grid.ClearSelection();
for(int r = 0; r < grid.RowCount; r++)
grid[columnIndex, r].Selected = true;
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.
// 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;
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:
But trying to change to type to Column Header Select or Column Select would result in the error you mentioned.
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.
I know this is a very old question. But I leave my solution below for people who will encounter this error in the future.
You will get this error through properties(UI) in general. I mean when you do SelectionMode -> FullColumnSelect or ColumnHeaderSelect. You get it. For this reason, I suggest you to change the SelectionMode via code instead of UI.
My solution is as follows.
Give your data to DataGridView as SelectionMode.FullRowSelect or SelectionMode.RowHeaderSelect.
Make all columns not sortable in a loop.
Change the dataGridView's selection mode in code.
//1
dataGridView.DataSource = productList;
//2
for (int i = 0; i < dataGridView.Columns.Count; ++i)
dataGridView.Columns[i].SortMode = DataGridViewColumnSortMode.NotSortable;
//3
dataGridView.SelectionMode = DataGridViewSelectionMode.FullColumnSelect;
精彩评论