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

Popular posts from this blog

javascript - Laravel datatable invalid JSON response -

java - Exception in thread "main" org.springframework.context.ApplicationContextException: Unable to start embedded container; -

sql server 2008 - My Sql Code Get An Error Of Msg 245, Level 16, State 1, Line 1 Conversion failed when converting the varchar value '8:45 AM' to data type int -