当我编辑DataGridView
的单元格时,我想立即更新我的MSSQL数据库。我添加了CellEndEdit' event handler. Also I added autogenerated bindingsource and
TableAdapter`。
它工作正常但每当我想编辑我的值时,我都会收到错误。实际上,我用SqlDataAdapter
编写了一个更新方法。但是在这个时候我使用了自动生成的TableAdapter
。我不知道该怎么办。我尝试了这个,但是我收到了一个错误。
注意:DataSet11
,loginTableAdapter
和Bindingsource1
是自动生成并正常工作。
private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
SqlCommandBuilder querybuilder = new SqlCommandBuilder(loginTableAdapter);
querybuilder.GetUpdateCommand();
loginTableAdapter.Update(dataset11);
}
答案 0 :(得分:0)
private void dataGridView1_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
{
if (dataGridView1.IsCurrentCellDirty)
{
SqlCommandBuilder querybuilder = new SqlCommandBuilder(loginTableAdapter);
querybuilder.GetUpdateCommand();
loginTableAdapter.Update(dataset11);
}
}
//or if you has a bindingSource
private void bindingSource1_CurrentItemChanged(object sender, EventArgs e)
{
if (dataGridView1.IsCurrentCellDirty)
{
SqlCommandBuilder querybuilder = new SqlCommandBuilder(loginTableAdapter);
querybuilder.GetUpdateCommand();
loginTableAdapter.Update(dataset11);
}
}