以下是全選、取消、批量刪除、批量恢復的部分代碼。
//以下是全選 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("刪除成功!", "提示"); }
|