Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 强制从筛选的DataGridView更新到数据库_C#_Sql Server 2008 - Fatal编程技术网

C# 强制从筛选的DataGridView更新到数据库

C# 强制从筛选的DataGridView更新到数据库,c#,sql-server-2008,C#,Sql Server 2008,我创建了一个应用程序来解决从机器传输数据时的日期转换问题。转换非常有效。我创建了一个包含所有记录的DataGridView。然后,我对DataGridView进行排序,以显示需要使用SqlDataAdapter和DataSet更新的数据。然后对DataGridView中的数据进行更改,并单击保存按钮将更改应用于数据库表 try { MessageBox.Show("Complete"); this.log5DataGrid

我创建了一个应用程序来解决从机器传输数据时的日期转换问题。转换非常有效。我创建了一个包含所有记录的DataGridView。然后,我对DataGridView进行排序,以显示需要使用SqlDataAdapter和DataSet更新的数据。然后对DataGridView中的数据进行更改,并单击保存按钮将更改应用于数据库表

     try
        {
            MessageBox.Show("Complete");
            this.log5DataGridView.EndEdit();
            //this.log5BindingSource.DataSource = this.log5DataGridView.DataSource; <-- Last Resort
            this.log5BindingSource.ResetBindings(false);
            this.log5BindingSource.EndEdit();
            this.log5TableAdapter.Update(roboticlineDataSet.Log5);
        }
        catch { }
问题是,如果datagridview表保留了更改,但log5BindingSource仍然包含原始更改,那么是否有办法将搜索到的log5DataGridView记录保存回数据库

this.log5TableAdapter.Fill(this.roboticlineDataSet.Log5);

谢谢。

当我编程datagridview以显示搜索的数据时,发现发生了什么,它没有更改bindingsource,但在表单加载()中保持不变

为了解决这个问题,我在Soultion资源管理器的Dataset.xsd位置找到了RobotLineTable。在这里,我选择了RobotlineTableAdapter并右键单击我选择的添加->查询的位置。在这里,我创建了一个带有where ID=@值的SQL查询,以执行所需的筛选/搜索。这将使用此代码替换using SqlDataAdapter代码

this.log5DataGridView.EndEdit();
this.log5BindingSource.EndEdit();    
this.log5tableAdapter.FillByID(this.roboticline.Log5, Value);
this.invalidate();
this.log5DatagridView.Refresh();
现在,当我希望保存时,BindingSource在datagridview中进行了更改,并更新/保存到数据库。创建和重新填充tableadapter有助于为您保留到bindingsource的链接


希望这能帮助将来可能遇到这种情况的其他人发现发生了什么,当我编程datagridview以显示搜索到的数据时,它没有更改bindingsource,但在表单加载()中保持不变

为了解决这个问题,我在Soultion资源管理器的Dataset.xsd位置找到了RobotLineTable。在这里,我选择了RobotlineTableAdapter并右键单击我选择的添加->查询的位置。在这里,我创建了一个带有where ID=@值的SQL查询,以执行所需的筛选/搜索。这将使用此代码替换using SqlDataAdapter代码

this.log5DataGridView.EndEdit();
this.log5BindingSource.EndEdit();    
this.log5tableAdapter.FillByID(this.roboticline.Log5, Value);
this.invalidate();
this.log5DatagridView.Refresh();
现在,当我希望保存时,BindingSource在datagridview中进行了更改,并更新/保存到数据库。创建和重新填充tableadapter有助于为您保留到bindingsource的链接


希望这对将来可能遇到这种情况的其他人有所帮助

当然,使用空的try/catch是隐藏任何异常并让所有人对任何潜在错误一无所知的完美方法。我建议删除空的try/catch。空的try/catch可以用Exception或DBCurrency和Finally进行归档。这不是问题,问题如上所述。数据更改未从datagridview传递到bindingsource。。删除try-catch仍然可以让它在程序中一直运行到结束,而不会出现任何异常。空的try/catch用于调试模式,因此无论发生什么异常,都可以停止并处理它,而不是继续在代码中使用错误数据。当然,使用空的try/catch是隐藏任何异常并让所有人都处于错误状态的完美方法对任何潜在的错误都要保密。我建议删除空的try/catch。空的try/catch可以用Exception或DBCurrency和Finally进行归档。这不是问题,问题如上所述。数据更改未从datagridview传递到bindingsource。。删除try-catch仍然可以让它在程序中一直运行到结束,而不会出现任何异常。空的try/catch用于调试模式,因此无论发生什么异常,都可以停止并处理它,而不是继续在代码中使用错误数据。当然,使用空的try/catch是隐藏任何异常并让所有人都处于错误状态的完美方法对任何潜在的错误都要保密。我建议删除空的try/catch。空的try/catch可以用Exception或DBCurrency和Finally进行归档。这不是问题,问题如上所述。数据更改未从datagridview传递到bindingsource。。删除try-catch仍然可以让它在程序中一直运行到结束,而不会发生任何情况。空的try/catch用于调试模式,因此无论发生什么异常,都可以停止并处理它,而不是继续通过包含坏数据的代码。
this.log5DataGridView.EndEdit();
this.log5BindingSource.EndEdit();    
this.log5tableAdapter.FillByID(this.roboticline.Log5, Value);
this.invalidate();
this.log5DatagridView.Refresh();