给出以下代码:
DialogResult result3 = MessageBox.Show("Proceed With Checkout?", "Request Granted",
MessageBoxButtons.YesNoCancel,
MessageBoxIcon.Question,
MessageBoxDefaultButton.Button2);
if (result3 == DialogResult.Yes)
{
int delete = int.Parse("s_NTextBox.Text");
this.BindingContext[iTEMDataSet, "ITEM"].RemoveAt(0);
}
if (result3 == DialogResult.No)
{
//quantity = quantity - 1;
//quantityTextBox.Text = Convert.ToString(quantity);
}
}
catch
{
MessageBox.Show("error");
}
}
在this.BindingContext[iTEMDataSet, "ITEM"].RemoveAt(0)
行,我想删除当前选中的项目。当前代码始终删除第一个项目。怎么做?
答案 0 :(得分:1)
使用Position
属性。
this.BindingContext[iTEMDataSet, "ITEM"].
RemoveAt(this.BindingContext[iTEMDataSet, "ITEM"].Position);
答案 1 :(得分:0)
据我所知,您将从数据库中删除与您在文本框中的给定ID匹配的记录。
所以,如果DialogResult.Yes为真,请写下面的代码
int numberOfRowsAffected = 0;
bool isDeleted = false;
string query = @"DELETE FROM targetTable WHERE ID=@ID";
SqlCommand cmd = new SqlCommand();
cmd.CommandText = query;
cmd.CommandType = System.Data.CommandType.Text;
cmd.Parameters.Add(new SqlParameter("@ID", deletedItemID));
try
{
using (SqlConnection cn = new SqlConnection(ConnectionString))
{
cmd.Connection = cn;
cn.Open();
object result = cmd.ExecuteNonQuery();
isDeleted = Convert.ToInt32(result) > 0 ? true : false;
}
}
catch (Exception ex)
{
isDeleted = false;
}