views:

417

answers:

2

I 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 DataGridViewColumnSortMode.Automatic.
+1  A: 

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;
SLaks
Nop... grid.ClearSelection(); works but second line doesn't...
Y_Y
How can I get rid of Automatic Sorting?
Y_Y
Set the `SortMode` property to `false`, as in David's answer.
SLaks
Works but I'm curious if my program will slow down if I have a heck of a lots of cells...
Y_Y
It might; try it.
SLaks
+1  A: 

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;
David Stratton
You can also call the `ClearSelection` method.
SLaks
Do I have to run that loop every time I want to select a Column? or is it on time call?
Y_Y
As long as nothing else changes `SortMode`, you only need to do that once.
SLaks
Lol when I click another column it selects all cells from the new column clicked.
Y_Y
Yes; that's what `FullColumnSelect` means. Read the documentation: http://msdn.microsoft.com/en-us/library/8x6w9028%28VS.80%29.aspx
SLaks