c# - Unable to fetch number of selected Columns or Rows in DataGridView -


i have following piece of code:

public double[] extractgriddata(datagridview grid)     {         numcells = grid.selectedcells.count;         numberofrows = grid.selectedrows.count;         numberofcolumns = grid.selectedcolumns.count;          double[] cellsdata = new double[numcells];              foreach (datagridviewcell cell in grid.selectedcells)             {                 if (cell.value != null)                     cellsdata[cell.rowindex] = convert.todouble(cell.value);             }             messagebox.show(numberofrows.tostring());         return cellsdata;     } 

i tried using following code:

int32 selectedrowcount = grid.rows.getrowcount(datagridviewelementstates.selected); 

i number of total cells, nut not number or selected rows or columns. might problem be?

if you're not getting selectedrows populated, may not have correct selectionmode set (been there, done that);

the selectionmode property must set fullrowselect or rowheaderselect selectedrows property populated selected rows.

if need column , row selection, not entire rows or columns, you're better off using selectedcells populated whichever selectionmode set.

edit: if need number of selected rows , columns, may (untested since don't have windows box handy)

int xsize = 0, ysize = 0; var b = a.selectedcells.cast<datagridviewcell>().tolist(); if (b.any()) {     ysize = b.max(x => x.rowindex) - b.min(x => x.rowindex) + 1;     xsize = b.max(x => x.columnindex) - b.min(x => x.columnindex) + 1; } 

note if select single cell @ (1,1) , cell @ (7,7), you'll size 7x7, if not cells in range selected.


Comments

Popular posts from this blog

Perl - how to grep a block of text from a file -

delphi - How to remove all the grips on a coolbar if I have several coolbands? -

javascript - Animating array of divs; only the final element is modified -