datagridview 上增删改
1.删除
private void button3_Click(object sender, EventArgs e)//删除 { try { #region //foreach (DataGridViewRow dgvRow in dataGridView1.SelectedRows) //{ // dataGridView1.Rows.Remove(dgvRow); //} //sda.Update(dt); // MessageBox.Show("记录删除成功!","信息提示",MessageBoxButtons.OK,MessageBoxIcon.Information); #endregion conn = new SqlConnection("database=test;server=(local);integrated security=true"); string sql = "delete from Table1 where 学号='" + this.dataGridView1.CurrentRow.Cells["学号"].Value.ToString().Trim() + "'"; conn.Open(); cmd = new SqlCommand(sql, conn); cmd.ExecuteNonQuery(); conn.Close(); MessageBox.Show("记录删除成功!", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information); sda = new SqlDataAdapter("select * from Table1", conn); scb = new SqlCommandBuilder(sda); dt = new DataTable("Table1"); try { sda.Fill(dt); dt.PrimaryKey = new DataColumn[] { dt.Columns[0] }; dataGridView1.DataSource = dt; dataGridView1.AlternatingRowsDefaultCellStyle.BackColor = Color.LightBlue; } catch (SqlException ex) { MessageBox.Show(ex.ToString()); } } catch (Exception ex) { MessageBox.Show(ex.Message, "删除不成功!"); } }
2.修改
private void button5_Click(object sender, EventArgs e)//修改 { string sql = "update Table1 set 姓名='" + this.dataGridView1.CurrentRow.Cells["姓名"].Value.ToString().Trim() + "'," + "性别='" + this.dataGridView1.CurrentRow.Cells["性别"].Value.ToString().Trim() + "'," + "年龄='" + this.dataGridView1.CurrentRow.Cells["年龄"].Value + "'," + "家庭地址='" + this.dataGridView1.CurrentRow.Cells["家庭地址"].Value.ToString().Trim() + "'"+ "where 学号='"+this.dataGridView1.CurrentRow.Cells["学号"].Value.ToString().Trim()+"'"; conn.Open(); cmd = new SqlCommand(sql,conn); cmd.ExecuteNonQuery(); conn.Close(); MessageBox.Show("修改成功!","提示"); }
3.保存
private void button4_Click(object sender, EventArgs e)//保存 { string sql1 = "insert into Table1 values(@学号,@姓名,@性别,@年龄,@家庭地址)"; btnsummit(sql1); conn.Close(); MessageBox.Show("添加库房信息成功!", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information); }
4.函数
#region 新增函数 private void btnsummit(string sql) { //将命令类型转为存储类型 conn = new SqlConnection("database=test;server=(local);integrated security=true"); conn.Open(); //利用Command对象调用存储过程 cmd = new SqlCommand(sql, conn); cmd.CommandType = CommandType.Text; //往存储过程中添加参数 cmd.Parameters.Add("@学号", SqlDbType.NVarChar, 12); cmd.Parameters.Add("@姓名", SqlDbType.NVarChar, 8); cmd.Parameters.Add("@性别", SqlDbType.NVarChar, 10); cmd.Parameters.Add("@年龄", SqlDbType.Int); cmd.Parameters.Add("@家庭地址", SqlDbType.NVarChar, 50); //给存储过程的参数付值 cmd.Parameters["@学号"].Value = this.dataGridView1.CurrentRow.Cells["学号"].Value.ToString().Trim(); cmd.Parameters["@姓名"].Value = this.dataGridView1.CurrentRow.Cells["姓名"].Value.ToString().Trim(); cmd.Parameters["@性别"].Value = this.dataGridView1.CurrentRow.Cells["性别"].Value.ToString().Trim(); cmd.Parameters["@年龄"].Value = this.dataGridView1.CurrentRow.Cells["年龄"].Value.ToString().Trim(); cmd.Parameters["@家庭地址"].Value = this.dataGridView1.CurrentRow.Cells["家庭地址"].Value.ToString().Trim(); cmd.ExecuteNonQuery();//判断修改成功 } #endregion