更新单元格中的错误

时间:2013-01-15 14:54:02

标签: c# datagridview tableadapter

当我编辑DataGridView的单元格时,我想立即更新我的MSSQL数据库。我添加了CellEndEdit' event handler. Also I added autogenerated bindingsource and TableAdapter`。

它工作正常但每当我想编辑我的值时,我都会收到错误。实际上,我用SqlDataAdapter编写了一个更新方法。但是在这个时候我使用了自动生成的TableAdapter。我不知道该怎么办。我尝试了这个,但是我收到了一个错误。

注意DataSet11loginTableAdapterBindingsource1是自动生成并正常工作。

private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
   SqlCommandBuilder querybuilder = new SqlCommandBuilder(loginTableAdapter);
   querybuilder.GetUpdateCommand();
   loginTableAdapter.Update(dataset11);
}

1 个答案:

答案 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);
            }
        }