c# - Find the highest ColumnIndex in DataGridView -
if select more 1 cell in datagridview, how can (in msgbox) value of cell highest columnindex (amongst selected) ?
list<string> = new list<string>(); foreach (datagridviewcell cell in dgvc.selectedcells) { a.add(cell.value.tostring()); } so, need rest of cells in row populate repeating list made selecting cells.
if select more 1 cell in datagridview, how can (in msgbox) value of cell highest columnindex (among selected)
one way use linq order collection of selected cells columnindex , first one.
var selectedcells =dgvc.selectedcells; var maxcolumn = (from datagridviewcell cell in selectedcells orderby cell.columnindex descending select cell).firstordefault(); messagebox.show(maxcolumn.value.tostring()); you should note depending on how user made selection (top bottom or bottom top) either value first row or last row. if matters should order rowindex
another way examine first , last cells in collection , compare indexes . need @ both because depending on how user made selection 1 greater column index change.
var selectedcells = dgvc.selectedcells; var cellstart = selectedcells[0]; var cellend = selectedcells[selectedcells.count -1]; if (cellstart.columnindex > cellend .columnindex) messagebox.show(cellstart.value.tostring()); else messagebox(cellend.value.tostring()); note: should check null values if allowuserstoaddrow true
Comments
Post a Comment