C# WinForm DataGridView Update, Insert, Delete Not Working -


here code :

public partial class form1 : form {     sqlcommandbuilder cmbl;     string con = "data source=localhost;initial catalog=db;persist security info=true;user id=sa;password=1234";     sqldataadapter sdahfp;     string querydgvhfp = "select * hfp";     datatable dthfp;     sqlconnection cn;      public form1()     {         initializecomponent();     }     private void form1(object sender, eventargs e)      {         sdahfp = new sqldataadapter(querydgvhfp, con);         dthfp = new datatable();         sdahfp.fill(dthfp);          foreach (datarow item in dthfp.rows)         {             int n = dgvhfp.rows.add();             dgvhfp.rows[n].cells[0].value = item["hfp"].tostring();             dgvhfp.rows[n].cells[1].value = item["yearperiod"].tostring();             if (item["active"].tostring() == "y")             {                 dgvhfp.rows[n].cells[2].value = true;             }             else             {                 dgvhfp.rows[n].cells[2].value = false;             };             dgvhfp.rows[n].cells[3].value = item["id"].tostring();         }     } 

that's load data sql query datagridview, , add button execute update or insert within code :

private void btupdate_click(object sender, eventargs e) {     try     {         cmbl = new sqlcommandbuilder(sdahfp);         sdahfp.update(dthfp);         messagebox.show("success", "update", messageboxbuttons.ok, messageboxicon.information);     }     catch (exception ex)     {         messagebox.show(ex.message, "error", messageboxbuttons.ok, messageboxicon.error);     } } 

when update / insert data in datagridview , click update button, success messagebox appears, data in database not updated nor inserted

please me solve problem, why update button doesn't work?

thank much.

you should bind datagridview datatable, when change cell value or add or remove rows, changes apply on datatable. can save data table changes using tableadapter:

this.datagridview1.datasource = datatable; 

note: store true/false or yes/no values, it's better use bit data type in sql server. in below example supposed need store values y/n nvarchar(50) , setup datagridviewcheckboxcolumn support editing string column.

example

suppose have table table1:

column1: int, primary key column2: nvarchar(50), allows null, containing y or n vaule 

and want edit data of table1 in datagridview:

enter image description here

here code can use load , edit , save data:

private datatable table;  private sqldataadapter dataadapter;  private void sampleform_load(object sender, eventargs e) {     var command = "select * table1";     var connection = @"your connection string";     table = new datatable();     dataadapter = new sqldataadapter(command, connection);     var commandbuilder = new sqlcommandbuilder(dataadapter);     dataadapter.fill(table);      //set false default value column2     table.columns["column2"].defaultvalue = false;      var column1 = new datagridviewtextboxcolumn();     column1.name = "column1";     column1.datapropertyname = "column1";      var column2 = new datagridviewcheckboxcolumn();     column2.name = "column2";     column2.datapropertyname = "column2";     column2.truevalue = "y";     column2.falsevalue = "n";      this.datagridview1.columns.add(column1);     this.datagridview1.columns.add(column2);      //bind grid table     this.datagridview1.datasource = table; }  private void savebutton_click(object sender, eventargs e) {     //save data     this.datagridview1.endedit();     dataadapter.update(table); } 

Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

javascript - Get parameter of GET request -

javascript - Twitter Bootstrap - how to add some more margin between tooltip popup and element -