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

c++ - Function signature as a function template parameter -

algorithm - What are some ways to combine a number of (potentially incompatible) sorted sub-sets of a total set into a (partial) ordering of the total set? -

How to call a javascript function after the page loads with a chrome extension? -