c# - Rows cannot be added programatically in Gridview. Index Error -


hi want retrieve data database gridview. using custom grid here c# code

try {     int i=0;     mysqlcommand cmd = new mysqlcommand("select accounts,credit,debit,sum(credit) c,sum(debit) d tbl_open_balance", con);     dr=cmd.executereader();     while(dr.read())     {         datagridview1.rows[i].cells[0].value=dr["accounts"].tostring();         datagridview1.rows[i].cells[1].value = dr["credit"].tostring();         datagridview1.rows[i].cells[2].value = dr["debit"].tostring();         datagridview1.rows.add();         i++;     }     datagridview1.borderstyle = borderstyle.none;     datagridview1.alternatingrowsdefaultcellstyle.backcolor = color.fromargb(238,239,249);     datagridview1.cellborderstyle = datagridviewcellborderstyle.singlehorizontal;     datagridview1.defaultcellstyle.selectionbackcolor = color.darkturquoise;     datagridview1.defaultcellstyle.selectionforecolor = color.whitesmoke;     datagridview1.backgroundcolor = color.white;      datagridview1.enableheadersvisualstyles = false;     datagridview1.columnheadersborderstyle = datagridviewheaderborderstyle.none;     datagridview1.columnheadersdefaultcellstyle.backcolor = color.fromargb(20, 25, 72);     datagridview1.columnheadersdefaultcellstyle.forecolor = color.white;     dr.close();  } catch (exception ex) {     dr.close(); } 

i received error index out of range. must non-negative , less size of collection. parameter name: index

i support khans proposal. try catching row datareader in string array , add datagridview.rows.add (myarray); grid. leave grid take , assign values next row.


Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -