c# - Crash when gerate click by a button in an empty gridview list -
when gerate click in gridview code :
datagridview1_cellclick(datagridview1, new datagridviewcelleventargs(0, 0));
on empty gridview, error occurs:
"an unhandled exception of type 'system.argumentoutofrangeexception' occurred in mscorlib.dll additional information: index out of range. must non-negative , less size of collection."
on line of code:
datagridviewrow row = this.datagridview1.rows[e.rowindex];
my question how avoid error? thank you!
in datagridview1_cellclick
, need check index greater 0 before attempt access element rows
.
private void datagridview1_cellclick(object sender, datagridviewcelleventargs e) { //don't want execute when column header/row clicked (oob) if (e.rowindex < 0 || e.columnindex < 0) return; datagridviewrow row = this.datagridview1.rows[e.rowindex]; }
Comments
Post a Comment