DataGridView: How to select an entire Column and deselect everything else?

后端 未结 2 468
轮回少年
轮回少年 2020-12-19 09:17

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 ge

相关标签:
2条回答
  • 2020-12-19 10:15

    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;
    
    0 讨论(0)
  • 2020-12-19 10:18

    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;
    
    0 讨论(0)
提交回复
热议问题