C#对DataGridView中的数据进行添加、修改、删除操作
1private Boolean dbUpdate()
2 {
3 string strSql = "select ProductsSpecID,ProductsSpec,ProductsSpecUnit,ProductsID from tbl_product_detail_master";
4 System.Data.DataTable dtUpdate = new System.Data.DataTable();
5 dtUpdate = this.dbconn(strSql);
6 dtUpdate.Rows.Clear();
7
8 System.Data.DataTable dtShow = new System.Data.DataTable();
9 //dtShow = (DataTable)this.bindSource.DataSource;
10 dtShow = (System.Data.DataTable)this.dgselect.DataSource;
11
12 int p1 = dtShow.Rows.Count;
13 // try
14 // {
15
16 for (int i = 0; i < dtShow.Rows.Count; i++)
17 {
18 DataRowState rowState=new DataRowState();
19 rowState=dtShow.Rows[i].RowState;
20 if (rowState==DataRowState.Added || rowState==DataRowState.Detached || rowState==DataRowState.Modified)
21 {
22
23 if (this.dgselect["Del", i].Value.ToString() == "1")
24 {
25 dtShow.Rows[i].Delete();
26 }
27 }
28 }
29
30 for (int i = 0; i < dtShow.Rows.Count; i++)
31 {
32 dtUpdate.ImportRow(dtShow.Rows[i]);
33 }
34 int num = dtUpdate.Rows.Count;
35 try
36 {
37 this.conn.Open();
38
39 trans = this.conn.BeginTransaction();
40
41 MySqlCommandBuilder CommandBuiler;
42 CommandBuiler = new MySqlCommandBuilder(this.adapter);
43
44 this.adapter.Update(dtUpdate);
45 trans.Commit();
46 this.conn.Close();
47
48 }
49 catch ( Exception ex)
50 {
51 MessageBox.Show(ex.Message.ToString());
52 trans.Rollback();
53 return false;
54 }
55
56 dtUpdate.AcceptChanges();
57 return true;
58
59 }
补充:软件开发 , C# ,