注意:在DataGridView添加一列(name:delete),ColumnType属性为:DataGridViewCheckBoxColumn,FlaseValue属性为:Flase,TureValue属性为Ture,并设置DataGridView的ReadOnly属性为Flase。
- //删除
- private void del_button_Click(object sender, EventArgs e)
- {
- try
- {
- int count = 0;
- for (int i = 0; i < form_dataGridView.RowCount; i++)
- {
- if (form_dataGridView.Rows[i].Cells[8].EditedFormattedValue.ToString() == "True")
- {
- count++;
- }
- }
- if (count == 0)
- {
- MessageBox.Show("请至少选择一条数据!", "提示");
- return;
- }
- else
- {
- if (MessageBox.Show(this, "共选择" + count + "条,你要删除这些数据吗?", "提 示", MessageBoxButtons.YesNo, MessageBoxIcon.Information).ToString() == "Yes")
- {
- string connStr = ConfigurationManager.ConnectionStrings["connStr"].ToString();
- OleDbConnection conn = new OleDbConnection(connStr);
- conn.Open();
- string str = "(";
- for (int i = 0; i < form_dataGridView.RowCount; i++)
- {
- if (form_dataGridView.Rows[i].Cells[8].EditedFormattedValue.ToString() == "True")
- {
- str += form_dataGridView.Rows[i].Cells[0].Value.ToString() + ",";
- }
- }
- if (str.Length > 0)
- {
- str = str.Substring(0, str.Length - 1);
- str += ")";
- MessageBox.Show(str);
- }
- string sql = "delete from form where id in " + str;
- OleDbCommand cmd = new OleDbCommand(sql, conn);
- cmd.ExecuteNonQuery();
- conn.Close();
- MessageBox.Show("删除成功");
- GetData();//引用事件,刷新数据
- }
- else
- {
- return;
- }
- }
- }
- catch
- {
- }
- }
时间: 2024-10-26 13:47:48