DataGridView: how to select the entire column and deselect everything else?

I tried to figure out how to select all the cells under the column with the right mouse button + menu + select this column ...

MSDN doesn't help much ...

I get this error when I try to change the selection mode:

DataGridView control SelectionMode cannot be set to FullColumnSelect while it has a   column with SortMode set to DataGridViewColumnSortMode.Automatic.

Thanks, Y_Y

+5
source share
3 answers

Scroll through the cells in the column and set their Selected property to true.
That sounds awful, but I believe this is 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;
+4
source

, - , , Visual Studio.

, :

foreach (DataGridViewColumn c in dataGridView1.Columns)
{
   c.SortMode = DataGridViewColumnSortMode.NotSortable;
   c.Selected = false;
}
dataGridView1.SelectionMode = DataGridViewSelectionMode.FullColumnSelect;
dataGridView1.Columns[0].Selected = true;
+9

3 .

  • .
  • , . - , .
  • .

, , . . , 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;
+1

All Articles