c# - Limiting DataGridView cell multi selection to only column or row -
i have datagridview number of columns , rows. have mutliselect
enabled, allows selection of cells.
i want limit selection vertically, horizontally, full row or full column, never mix of both. user can select dragging starting @ cell either vertically or horizontally.
here's small diagram clarify if helps @ all.
here's brute force method - when selection changes, de-select cells fall outside current row/column selection:
int _selectedrow = -1; int _selectedcolumn = -1; private void datagridview1_selectionchanged(object sender, eventargs e) { switch (datagridview1.selectedcells.count) { case 0: // store no current selection _selectedrow = -1; _selectedcolumn = -1; return; case 1: // store starting point multi-select _selectedrow = datagridview1.selectedcells[0].rowindex; _selectedcolumn = datagridview1.selectedcells[0].columnindex; return; } foreach (datagridviewcell cell in datagridview1.selectedcells) { if (cell.rowindex == _selectedrow) { if (cell.columnindex != _selectedcolumn) { _selectedcolumn = -1; } } else if (cell.columnindex == _selectedcolumn) { if (cell.rowindex != _selectedrow) { _selectedrow = -1; } } // otherwise cell selection illegal - de-select else cell.selected = false; } }
Comments
Post a Comment