我一直在處理這段代碼幾個小時,現在它顯示的信息框已被成功刪除,但它不會刪除數據庫我似乎無法找到我出錯的地方。從數據庫中刪除選定的行數據
private void simpleButton5_Click(object sender, EventArgs e)
{
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
DataGridViewRow delrow = dataGridView1.Rows[i];
if (delrow.Selected == true)
{
//A YesNo enabled messagebox
DialogResult dialogResult = DevExpress.XtraEditors.XtraMessageBox.Show("Are you sure you want delete the selected client's information?", " ", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
//An if statement for a yes selected button on the messagebox
if (dialogResult == DialogResult.Yes)
{
dataGridView1.Rows.RemoveAt(i);
try
{
Conn.Open();
SqlCommand Scd = new SqlCommand("Delete From Client WHERE ClientID=" + i + "" ,Conn);
Scd.ExecuteNonQuery();
Conn.Close();
DevExpress.XtraEditors.XtraMessageBox.Show("You have successfully deleted the selected client's information on the system", " ", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (Exception ex)
{
DevExpress.XtraEditors.XtraMessageBox.Show(ex.ToString());
}
}
//An if statement for a no selected button on the messagebox
else if (dialogResult == DialogResult.No)
{
//Closing this form and opening the main menu form
this.Hide();
MainMenu mm = new MainMenu();
mm.ShowDialog();
this.Close();
this.Dispose();
}
}
}
}
您已經將您提供給用戶的數據視圖與實際數據混合在一起。一個DataAdapter可以讓你擺脫所有的代碼:'myDA.Update(myDT);' – Plutonix