以下是全选、取消、批量删除、批量恢复的部分代码。
//以下是全选 bool IsSelectAll = false; private void allsel_Click(object sender, EventArgs e) { if (dataGridView1.Rows.Count > 0) { for (int j = 0; j < dataGridView1.RowCount; j++) { dataGridView1[0,j].Value = "true"; } IsSelectAll = true; return; } else { MessageBox.Show("已删除列表中没有任何记录!","提示",MessageBoxButtons.OK,MessageBoxIcon.Information); return; } }
//以下是取消选择 private void allcls_Click(object sender, EventArgs e) { if (dataGridView1.Rows.Count > 0) {
for (int j = 0; j < dataGridView1.RowCount; j++) { dataGridView1[0,j].Value = "false"; } IsSelectAll = false; return; } else { MessageBox.Show("已删除列表中没有任何记录!","提示",MessageBoxButtons.OK,MessageBoxIcon.Information); return; } }
//批量删除员工帐户 private void button4_Click(object sender, EventArgs e) { if (dataGridView1.Rows.Count > 0) { try { int count = 0; string[] arr = new string[dataGridView1.RowCount]; string deltxt = "delete Employeestore where WorkNo="; if (IsSelectAll) //全选后批量删除 { for (int j = 0; j < dataGridView1.RowCount; j++) { if (Convert.ToBoolean(dataGridView1[0, j].EditedFormattedValue.ToString())) //这里判断复选框是否选中 { arr[j] = dataGridView1["工号",j].Value.ToString(); count++; } } if (count == 0) { MessageBox.Show("请至少选择一条员工数据!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } else { if (MessageBox.Show("真的要删除选中的员工数据吗?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information).ToString() == "Yes") { for (int k = 0; k < dataGridView1.RowCount; k++) { if (arr[k] != null) { X_SqlClass.GetExecute("" + deltxt + arr[k] + ""); } } } else { return; } } } else //删除所选择的行 { for (int j = 0; j < dataGridView1.RowCount; j++) { if (Convert.ToBoolean(dataGridView1[0, j].EditedFormattedValue.ToString())) //这里判断复选框是否选中 { arr[j] = dataGridView1["工号",j].Value.ToString(); count++; } } if (count == 0) { MessageBox.Show("请至少选择一条员工数据!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } else { if (MessageBox.Show("真的要删除选中的员工数据吗?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information).ToString() == "Yes") { for (int k = 0; k < dataGridView1.RowCount; k++) { if (arr[k] != null) { X_SqlClass.GetExecute("" + deltxt + arr[k] + ""); } } } else { return; } } } } catch (Exception ex) { MessageBox.Show(ex.Message); } } else { MessageBox.Show("已删除列表中没有任何记录!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); return; }
Form1_Activated(sender,e); MessageBox.Show("删除成功!", "提示"); }
|