vb.net - DataGridView.ClearSelection() not working as expected -
in application used code dgvcases.clearselection() deselect rows. visually nothing selected first row selected. here code value "id" column of selected row.
try caseid = dgvcases.item("id", dgvcases.currentrow.index).value msgbox(caseid) catch ex exception msgbox("please select row", msgboxstyle.information) end try it getting "id" first row not wanted. going wrong , how can clearselection first row.
from msdn documentation on datagridview.clearselection, emphasis mine:
clears current selection unselecting all selected cells.
"now visually nothing selected" because nothing indeed selected. using currentrow.index presumably value selected row. currentrow , currentcell not equate row/cell being selected. common misconception.
from comments section of above linked answer:
currentcellreturns "active" cell, different "selected". if there multiple rows selected, active cell might somewhere else, , there can 1 active cell – luke marlinand also, if de-select in data grid view, 1 of cells still active, can't rely on being null or there being no rows selected. – daniel gray
your best bet replace
caseid = dgvcases.item("id", dgvcases.currentrow.index).value with
caseid = dgvcases.item("id", dgvcases.selectedrows(0).index).value after clearing selection, trigger catch block. side note: use if/else instead of try/catch verify there selected rows.
Comments
Post a Comment