C#對DataGridView進行添加、修改、刪除資料操作(裝載)

來源:互聯網
上載者:User

資料庫用的是本地服務器(MySql):

設定全域變數:

        MySqlConnection conn;
        MySqlDataAdapter adapter;
        MySqlTransaction trans;

1.  // 資料庫聯結

        private System.Data.DataTable dbconn(string strSql)
        {
            string strconn = "host=localhost;database=test;user id=root;password=";
            conn = new MySqlConnection();
            conn.ConnectionString = strconn;
            conn.Open();
            this.adapter = new MySqlDataAdapter(strSql, conn);
            System.Data.DataTable dtSelect = new System.Data.DataTable();
            int rnt=this.adapter.Fill(dtSelect);
            conn.Close();
            return dtSelect;
                              
        }

2.       //設定DataGridView的樣式 

       private void setDgStyle()
        {
            this.dgselect.Columns.Clear();

            DataGridViewCheckBoxColumn colDel = new DataGridViewCheckBoxColumn();
            colDel.DataPropertyName = "Del";
            colDel.Name = "Del";
            colDel.Selected = false;
            colDel.FalseValue = "0";
            colDel.TrueValue = "1";
            colDel.Width = 40;
            colDel.SortMode = DataGridViewColumnSortMode.NotSortable;
            colDel.HeaderText = "刪除";
            colDel.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
            colDel.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
            this.dgselect.Columns.Insert(0, colDel);

            DataGridViewTextBoxColumn colID = new DataGridViewTextBoxColumn();
            colID.DataPropertyName = "ProductsSpecID";
            colID.Name = "ProductsSpecID";
            colID.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
            colID.HeaderText = "產品規格ID";
            colID.Width = 160;
            this.dgselect.Columns.Insert(1, colID);

            DataGridViewTextBoxColumn colNM = new DataGridViewTextBoxColumn();
            colNM.DataPropertyName = "ProductsSpec";
            colNM.Name = "ProductsSpec";
            colNM.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
            colNM.HeaderText = "產品規格名稱";
            colNM.Width = 160;
            this.dgselect.Columns.Insert(2, colNM);

            DataGridViewTextBoxColumn colUnit = new DataGridViewTextBoxColumn();
            colUnit.DataPropertyName = "ProductsSpecUnit";
            colUnit.Name = "ProductsSpecUnit";
            colUnit.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
            colUnit.HeaderText = "產品規格單位";
            colUnit.Width = 180;
            this.dgselect.Columns.Insert(3, colUnit);

            DataGridViewTextBoxColumn colPID = new DataGridViewTextBoxColumn();
            colPID.DataPropertyName = "ProductsID";
            colPID.Name = "ProductsID";
            colPID.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
            colPID.HeaderText = "產品ID";
            colPID.Width = 140;
            this.dgselect.Columns.Insert(4, colPID);

            DataGridViewButtonColumn colButton = new DataGridViewButtonColumn();
            colButton.DataPropertyName = "colSearch";
            colButton.Name = "colSearch";
            colButton.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
            colButton.HeaderText = "Button";
            colButton.Width = 80;
            this.dgselect.Columns.Insert(5, colButton);

            this.dgselect.RowHeadersWidth = 15;
            this.dgselect.ColumnHeadersDefaultCellStyle.Font=new System.Drawing.Font("宋體",14);
                    
        }

3.   //修改資料,並將數據提交到資料庫

private Boolean dbUpdate()
        {
            string strSql = "select ProductsSpecID,ProductsSpec,ProductsSpecUnit,ProductsID from                       tbl_product_detail_master";
            System.Data.DataTable dtUpdate = new System.Data.DataTable();
            dtUpdate = this.dbconn(strSql);
            dtUpdate.Rows.Clear();

            System.Data.DataTable dtShow = new System.Data.DataTable();
            //dtShow = (DataTable)this.bindSource.DataSource;
            dtShow = (System.Data.DataTable)this.dgselect.DataSource;

            int p1 = dtShow.Rows.Count;
           // try
           // {

                for (int i = 0; i < dtShow.Rows.Count; i++)
                {
                   DataRowState rowState=new DataRowState();
                   rowState=dtShow.Rows[i].RowState;
                   if (rowState==DataRowState.Added || rowState==DataRowState.Detached || rowState==DataRowState.Modified)
                    {

                        if (this.dgselect["Del", i].Value.ToString() == "1")
                        {
                            dtShow.Rows[i].Delete();
                        }
                    }
                }
               
            for (int i = 0; i < dtShow.Rows.Count; i++)
            {
                dtUpdate.ImportRow(dtShow.Rows[i]);
            }
            int num = dtUpdate.Rows.Count;
            try
            {
                this.conn.Open();
               
                trans = this.conn.BeginTransaction();

                MySqlCommandBuilder CommandBuiler;
                CommandBuiler = new MySqlCommandBuilder(this.adapter);

                this.adapter.Update(dtUpdate);
                trans.Commit();
                this.conn.Close();

            }
            catch ( Exception  ex)
            {
                MessageBox.Show(ex.Message.ToString());
                trans.Rollback();
                return false;
            }

            dtUpdate.AcceptChanges();
             return true;

        }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.