问题
My program outputs information froma DataTable I collect form sql data into a DataGridview and the last column in the DGV is a checkbox. I want to know if there's any quick way/event I could use so whenever a user clicks and drags to make a large selection (like in the image, clicking on the second cell and dragging down), all the selected cells change their status as if the user had clicked on each individual checkbox?
The goal is to have a quick bulk-edit way to check large selections since that's a lot easier than checking each individual checkbox.
If there's no event handler for drag-selecting, how would I get the bounds of a large selection (start cell, end cell)?

回答1:
As the standard behaviour deosn't work with DGV CheckBoxesCells without a little coding here is how I would do it, staying with the normal Windows UI style, that is checking (and unchecking) CheckBoxes
with the Space
bar:
private void DGV_KeyDown(object sender, KeyEventArgs e)
{
if( e.KeyCode == Keys.Space && DGV.CurrentCell.ColumnIndex == yourCheckBoxColumn)
{
foreach (DataGridViewCell cell in DGV.SelectedCells)
if (cell.ColumnIndex == yourCheckBoxColumn)
cell.Value = !((bool)cell.FormattedValue);
}
}
I repeat the check to avoid crashes on cross selections..
Note that I decided to inverse each Checkmark; this way even complicated selection can be achived easily. I like that. Your mileage may vary: You may e.g. want to pick out the first Cell and set all cells to the opposite of its value.. This really depends on your users!
来源:https://stackoverflow.com/questions/26851554/c-sharp-change-datagridview-cells-by-dragging-on-them