DataSet DataSet , the data is cached in client memory and supports a disconnected connection. When the DataGridView control binds to a dataset, it automatically changes the state of the DS's rows, and when doing additions and deletions, it can be done using the SqlCommandBuilder class. SqlCommandBuilder must execute the SELECT command to retrieve the metadata, so it requires multiple round trips to the server, which increases the overhead of the application and the table must have a PRIMARY KEY constraint. The advantage is the automatic establishment of InsertCommand and other commands1, add an actionPrivate voidButton2_Click (Objectsender, EventArgs e) { using(SqlConnection con =NewSqlConnection (connstring)) {con. Open (); Dapt=NewSqlDataAdapter ("Select Stucode,spcode,ctcode,stunits from Stunits", con); SqlCommandBuilder SDB=NewSqlCommandBuilder (DAPT); DataSet DS1=ds. GetChanges (); if(ds. HasChanges ()) {Try{dapt. Update (DS,"Stunit"); MessageBox.Show ("saved successfully","Tips"); } Catch(SqlException ex) {MessageBox.Show (ex). Message); } } } This. datagridview1.allowusertoaddrows =false; }2) Delete operation (select Entire row)Private voidDelete Toolstripmenuitem_click (Objectsender, EventArgs e) { using(SqlConnection con =NewSqlConnection (connstring)) { //con. Open ();StringBuilder SB =NewStringBuilder (); Sb. Append ("Delete stunits where stucode= '"+ This. datagridview1.selectedcells[0]. Value.tostring () +"'"); SqlCommand cmd=NewSqlCommand (sb.) ToString (), con); Dapt. DeleteCommand=cmd; introwindex = This. DataGridView1.CurrentCell.RowIndex; Ds. tables["Stunit"]. Rows[rowindex]. Delete ();//Modify the state of a row//string celltext = This.datagridview1.selectedcells[rowindex]. Value.tostring (); //MessageBox.Show (celltext); if(ds. HasChanges (datarowstate.deleted)) {Try{dapt. Update (DS,"Stunit"); MessageBox.Show ("Delete succeeded","Tips"); } Catch(SqlException ex) {MessageBox.Show (ex). Message); } } } }2) Delete operation (click the first single-no-grid delete)Private voidDatagridview1_cellclick (Objectsender, DataGridViewCellEventArgs e) { if(E.columnindex = =0&& E.rowindex >=0) { using(conn =NewSqlConnection (Con)) {Conn. Open (); StringBuilder SB=NewStringBuilder (); Sb. Append ("Delete zy_bbxmxx where code = '"+ This. Datagridview1.rows[e.rowindex]. cells[1]. Value.tostring () +"'"); //MessageBox.Show (sb.) ToString ());Dapt. DeleteCommand =NewSqlCommand (sb.) ToString (), conn); DialogResult Answer= MessageBox.Show ("whether to delete","Tips", Messageboxbuttons.yesno, messageboxicon.exclamation); if(Answer = =dialogresult.yes) {Try{ds. tables[0]. Rows[e.rowindex]. Delete ();//change the state of a rowDapt. Update (DS,"zy_bbxx"); MessageBox.Show ("Delete succeeded"); } Catch(SqlException ex) {MessageBox.Show (ex). Message); } } } } } 3) Modify the OperationPrivate voidModify Toolstripmenuitem_click (Objectsender, EventArgs e) { using(SqlConnection con =NewSqlConnection (connstring)) {con. Open (); //DataSet ds1 = ds. GetChanges ();DAPT =NewSqlDataAdapter ("Select Stucode,spcode,ctcode,stunits from Stunits", con); SqlCommandBuilder SDB=NewSqlCommandBuilder (DAPT); if(ds. HasChanges ()) {Try{dapt. Update (DS,"Stunit"); MessageBox.Show ("Modification succeeded","Tips"); } Catch(SqlException ex) {MessageBox.Show (ex). Message); }}}[reprint]dataset of the increase of the deletion check operation (DataGridView binding) orPrivate voidBtnedit_click (Objectsender, EventArgs e) { using(SqlConnection conn=NewSqlConnection (Con)) {DataSet DS1= ds. GetChanges ();//Get the modified dataset if(DS1! =NULL) { Try{dapt=NewSqlDataAdapter ("SELECT * from Zy_bbxmxx", conn); SqlCommandBuilder SB=NewSqlCommandBuilder (DAPT); Dapt. Update (DS,"zy_bbxx"); MessageBox.Show ("Modification succeeded"); } Catch(ArgumentNullException ex) {MessageBox.Show (ex). Message); } } } }4) query OperationPrivate voidTextbox5_textchanged (Objectsender, EventArgs e) { This. Datagridview2.datasource = ds. tables[1]; DataView DV= ds. tables[1]. DefaultView; Dv. RowFilter="stucode like '"+'%'+ This. Textbox5.text +'%'+"'"; This. Datagridview2.datasource = ds. tables[1]; }
DataSet increase and deletion check operation (DataGridView binding)