0
votes

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)?

enter image description here

1
Just to note: The Windows UI way to achieve this is the 1) select the cells 2) press space. Have you tried that? If it doesn't work out of the box this is what you should code for. You plan sounds non-standard and also rather non-intuitive..TaW
Just test it and found that it indeed doesn't seem to work out of the box..TaW
Tried it too, pressing spacebar only checks the last box you selected...ConnorU

1 Answers

1
votes

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!