以下代码尝试从datagridview中删除选定的行并更新数据库。
但它没有更新数据库...它只是发出错误,“CommandText属性尚未初始化。” ...有任何想法吗?我认为这是因为它在开始时没有受到限制,但此时我无能为力而且头部酸痛。
private void deleteRow()
{
DialogResult dr = MessageBox.Show("Are you sure you want to delete this row?", "Confirmation",
MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (dr == DialogResult.Yes)
{
while (dataGridView1.SelectedRows.Count > 0)
dataGridView1.Rows.Remove(dataGridView1.SelectedRows[0]);
try
{
this.Validate();
this.tradesBindingSource.EndEdit();
this.tradesTableAdapter.Update(this.tradesDataSet.Trades);
}
catch (Exception ex)
{
MessageBox.Show("An error occurred during the update process: " + ex);
// Add code to handle error here.
}
this.tradesTableAdapter.Fill(this.tradesDataSet.Trades); // refresh table
}
}
答案 0 :(得分:0)
我删除了我的表适配器,数据集等...并重新创建了整个表。用新数据填充它,并尝试删除一行。它起作用了......不知道为什么,但它现在有效。