/// <summary>
/// 刪除
/// </summary>
private void bindingNavigatorDeleteItem_Click(object sender, EventArgs e)
{
#region Shift多行刪除
if (this.dataGridView1.SelectedRows.Count > 0)
{
if (MessageBox.Show("確定要刪除選中的員工信息嗎?刪除后將不可恢復!", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
Del();
}
}
else
{
MessageBox.Show("請選擇要刪除的員工信息!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
#endregion
}
/// <summary>
/// 刪除的方法
/// </summary>
public void Del()
{
int k = this.dataGridView1.SelectedRows.Count;
if (dataGridView1.Rows.Count > 0)
{
for (int i = k; i >= 1; i--)//從下往上刪,避免沙漏效應
{
string usercode = dataGridView1.SelectedRows[i - 1].Cells["UCode"].Value.ToString();
if(userbll.DelUser(usercode)) //從數據庫執行刪除操作
{
}
else
{
MessageBox.Show("刪除失敗!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
this.dataGridView1.Rows.RemoveAt(dataGridView1.SelectedRows[i - 1].Index);
}
}
else
{
dataGridView1.Rows.Clear();
}
}
------------------------------------------------------------------
DataGridView 如何實現鼠標右鍵刪除行:
首先在程序中添加一個ContexMenuStrip1控件,給該控件添加刪除項,
把 DataGridView 的ContexMenuStrip屬性項綁定為你所添加的ContexMenuStrip1控件,然后就是你自己編寫程序來操作刪除具體的東西了。