Datagridview使用适配器删除行
本文关键字:删除行 适配器 Datagridview | 更新日期: 2023-09-27 18:04:18
我使用arapter.Update(Dataset)
创建/更新/删除应用程序。一切工作正常,但我正在寻找如何在update
执行之前添加问题对话框
当我点击删除按钮时,我想看到这样的问题对话框:
DialogResult delete = MessageBox.Show("are you sure you want to delete this ?", "Delete Confirmation", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
if (delete == DialogResult.OK)
{
MessageBox.Show("Deleted");
}
这段代码我想把下面的代码,但我不知道如何确定是否删除命令触发!
private void saveToolStripButton_Click(object sender, EventArgs e)
{
try
{
this.Validate();
this.bs.EndEdit();
cmb = new SqlCommandBuilder(dataAdapter);
dataAdapter.UpdateCommand = cmb.GetUpdateCommand();
dataAdapter.Update(this.ds, "grupe");
MessageBox.Show("Update successful");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
试试这个,
写一个以DataRowState
为参数的方法;
public DialogResult Dialog(DataRowState state)
{
if (state == DataRowState.Modified)
{
DialogResult update = MessageBox.Show("are you sure you want to update this ?", "Update Confirmation", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
return update;
}
if (state == DataRowState.Deleted)
{
DialogResult delete = MessageBox.Show("are you sure you want to delete this ?", "Delete Confirmation", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
return delete;
}
return DialogResult.None;
}
And at button click;
private void button1_Click(object sender, EventArgs e)
{
DataTable dt = dataTbl.GetChanges(); //it will return the changes
foreach (DataRow item in dt.Rows)
{
if (Dialog(item.RowState) == DialogResult.OK)
{
SqlCommandBuilder cmb = new SqlCommandBuilder(adp);
adp.UpdateCommand = cmb.GetUpdateCommand();
adp.Update(this.dataTbl);
}
}
}
DataTable
可以总是为空。在填充适配器后使用datatable.AcceptChanges()
希望帮助,