DataGridView进行添加、修改、删除数据操作-C#

时间:2021-06-05 22:10:15

private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if (dataGridView1.Columns["UpdateData"] != null)
{
if (e.ColumnIndex == dataGridView1.Columns["UpdateData"].Index)
{
dataGridView1.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.White;

string attend_id = dataGridView1[dataGridView1.Columns["编号"].Index, e.RowIndex].Value.ToString();
string time1 = dataGridView1[dataGridView1.Columns["起始时间"].Index, e.RowIndex].Value.ToString();
string time2 = dataGridView1[dataGridView1.Columns["结束时间"].Index, e.RowIndex].Value.ToString();
string num = dataGridView1[dataGridView1.Columns["节次"].Index, e.RowIndex].Value.ToString();
if (time1 != "" && num != "" && time2!="")
UpdateData1(attend_id, time1, time2, num);
}
else if (e.ColumnIndex == dataGridView1.Columns["del"].Index)
{
if (Class1.alert2("您确定要删除第(" + dataGridView1[dataGridView1.Columns["节次"].Index, e.RowIndex].Value.ToString() + "节次)吗?"))
{
string attend_id = dataGridView1[dataGridView1.Columns["编号"].Index, e.RowIndex].Value.ToString();
string sql = string.Format("DELETE FROM attend WHERE attend_id={0}", attend_id);
DataBase.conn(sql);
Bd1();
}
}
}
}

public static void BindDataGridView(System.Windows.Forms.DataGridView dg, DataTable dtGrid)
{
dg.Columns.Clear();
dtGrid.Columns[0].ReadOnly = true;
dg.DataSource = dtGrid;

//加入更改按钮
System.Windows.Forms.DataGridViewButtonColumn column_addbutton = new System.Windows.Forms.DataGridViewButtonColumn();
column_addbutton.HeaderText = "更改";
column_addbutton.Name = "UpdateData";
column_addbutton.Text = "更改";
column_addbutton.UseColumnTextForButtonValue = true;
column_addbutton.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.AllCells;
column_addbutton.CellTemplate.Style.BackColor = System.Drawing.Color.Honeydew;
dg.Columns.Add(column_addbutton);

//加入删除按钮
System.Windows.Forms.DataGridViewButtonColumn column_delbutton = new System.Windows.Forms.DataGridViewButtonColumn();
column_delbutton.HeaderText = "删除";
column_delbutton.Name = "del";
column_delbutton.Text = "删除";
column_delbutton.UseColumnTextForButtonValue = true;
column_delbutton.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.AllCells;
column_delbutton.CellTemplate.Style.BackColor = System.Drawing.Color.Honeydew;
dg.Columns.Add(column_delbutton);
}


private void button1_Click(object sender, EventArgs e)
{
//DataTable table = (DataTable)dataGrid1.DataSource;
//table.Rows[dataGrid1.CurrentRowIndex].Delete();
//dataGrid1.DataSource = dataSet1.Tables[0].DefaultView;
//dataSet1.Tables[0].DefaultView[dataGrid1.CurrentRowIndex].Delete();

//oleDbDataAdapter1.Fill(dataSet1, "tushu");
int current = dataGrid1.CurrentCell.RowNumber;
string tushuma = dataGrid1[current, 0].ToString();
string de = "delete* from tushu where 图书码='"+tushuma+"'";
OleDbCommand mycommand = new OleDbCommand(de,oleDbConnection1);
mycommand.CommandText = de;
//OleDbParameter parm = mycommand.Parameters.Add("@tushuma",OleDbType.VarChar,13,"tushuma");
oleDbConnection1.Open();
mycommand.ExecuteNonQuery();
oleDbConnection1.Close();

}


private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if (dataGridView1.Columns["UpdateData"] != null)
{
if (e.ColumnIndex == dataGridView1.Columns["UpdateData"].Index)
{
dataGridView1.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.White;

string attend_id = dataGridView1[dataGridView1.Columns["编号"].Index, e.RowIndex].Value.ToString();
string time1 = dataGridView1[dataGridView1.Columns["起始时间"].Index, e.RowIndex].Value.ToString();
string time2 = dataGridView1[dataGridView1.Columns["结束时间"].Index, e.RowIndex].Value.ToString();
string num = dataGridView1[dataGridView1.Columns["节次"].Index, e.RowIndex].Value.ToString();
if (time1 != "" && num != "" && time2!="")
UpdateData1(attend_id, time1, time2, num);
}
else if (e.ColumnIndex == dataGridView1.Columns["del"].Index)
{
if (Class1.alert2("您确定要删除第(" + dataGridView1[dataGridView1.Columns["节次"].Index, e.RowIndex].Value.ToString() + "节次)吗?"))
{
string attend_id = dataGridView1[dataGridView1.Columns["编号"].Index, e.RowIndex].Value.ToString();
string sql = string.Format("DELETE FROM attend WHERE attend_id={0}", attend_id);
DataBase.conn(sql);
Bd1();
}
}
}
}

C#对DataGridView中的数据进行添加、修改、删除操作
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;

        }