winforms - column count property cannot be set on a databound datagridview control c# -


in form have datagridview. , controls input , output data. while displaying data in gird view. calling bindgrid in 2 occasions. 1 while formload , other after adding new record.

 public formaccounts()     {         initializecomponent();         bindgrid();                }   private void bindgrid()     {         oledbconnection conn = new oledbconnection();         conn.connectionstring = @"provider=microsoft.jet.oledb.4.0;data source= g:\sanjeev\testdb\db.mdb;jet oledb:database password=test123; jet oledb:engine type=5";         conn.open();         string constring = @"provider=microsoft.jet.oledb.4.0;data source=g:\sanjeev\testdb\db.mdb;jet oledb:database password=test123; jet oledb:engine type=5";          using (oledbconnection con = new oledbconnection(constring))         {             using (oledbcommand cmd = new oledbcommand("select * tblaccounts", con))             {                 cmd.commandtype = commandtype.text;                 using (oledbdataadapter sda = new oledbdataadapter(cmd))                 {                     using (datatable dt = new datatable())                     {                         sda.fill(dt);                         //set autogeneratecolumns false                         dtgrdaccounts.autogeneratecolumns = false;                         //set columns count                         ***dtgrdaccounts.columncount = 3;***                         //add columns                         dtgrdaccounts.columns[0].name = "accountid";                         dtgrdaccounts.columns[0].headertext = "id";                         dtgrdaccounts.columns[0].datapropertyname = "accid";                          dtgrdaccounts.columns[1].headertext = "account name";                         dtgrdaccounts.columns[1].name = "account name";                         dtgrdaccounts.columns[1].datapropertyname = "accname";                          dtgrdaccounts.columns[2].name = "accountnumber";                         dtgrdaccounts.columns[2].headertext = "account number";                         dtgrdaccounts.columns[2].datapropertyname = "accnumber";                         dtgrdaccounts.datasource = dt;                     }                 }             }         }     } 

if remove line dtgrdaccounts.columncount = 3; getting below error. index out of range. must non-negative , less size of collection. parameter name: index

after adding new record , on button save calling bindgrid() method record in gridiview.

string cmdtext = "praddaccounts"; oledbcommand cmd = new oledbcommand(cmdtext, conn);             cmd.commandtype = commandtype.storedprocedure; cmd.parameters.addwithvalue("accname", txtaccname.text.tostring());  //..... other parameters  insert record ..... oledbcommand cmd = new oledbcommand(cmdtext, conn);             cmd.commandtype = commandtype.storedprocedure; oledbdatareader reader = cmd.executereader(); bindgrid(); 

the issue when call bindgrid() again have clear first:

string cmdtext = "praddaccounts"; oledbcommand cmd = new oledbcommand(cmdtext, conn);             cmd.commandtype = commandtype.storedprocedure; cmd.parameters.addwithvalue("accname", txtaccname.text.tostring());  //..... other parameters  insert record .....  //clear binding. dtgrdaccounts.datasource = null; //bind new data. bindgrid(); 

Comments

Popular posts from this blog

PHPMotion implementation - URL based videos (Hosted on separate location) -

javascript - Using Windows Media Player as video fallback for video tag -

c# - Unity IoC Lifetime per HttpRequest for UserStore -