程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> C#對DataGridView中的數據進行添加、修改、刪除操作

C#對DataGridView中的數據進行添加、修改、刪除操作

編輯:C#入門知識

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        }
 

    

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved