【发布时间】:2014-05-29 16:55:02
【问题描述】:
我有一个在后台运行的计时器,用于保持数据网格视图对象的数据更新。我的计时器正在调用 sqldatadapter.fill 来填充本地范围的数据集。
如果我转到 sql server 并从表中删除一行,我填充数据集的调用不会反映数据库中的这种更改。
如果我在数据库中更新非主键字段的值并且我的计时器运行,则更改将被拾取并显示在我的 datagridview 中。
我需要如何设置我的 sqldataadapter 以便在其他后端进程添加或删除行时它会发现?
这是在 form_load 中
If ds Is Nothing Then
ds = New DataSet()
End If
Try
sqldataadapter = New SqlClient.SqlDataAdapter(SELECTQUERY, parentconnectionstring)
sqldataadapter.MissingSchemaAction = MissingSchemaAction.AddWithKey
sqlcommandbuilder = New SqlClient.SqlCommandBuilder(sqldataadapter)
sqldataadapter.Fill(ds)
mydata = ds.Tables(0).DefaultView
Catch ex As Exception
ExceptionHandler(ex)
End Try
If mydata.Table.Rows.Count > 0 Then
bindingsource.DataSource = mydata
dgvSystemWeightConfig.DataSource = bindingsource
UpdateLastUpdateTime()
End If
这是我的刷新程序
Public Sub refreshdata()
disableupdatetimer()
Try
If dgvSystemWeightConfig.IsCurrentCellInEditMode Then
Debug.WriteLine("tried to refresh system weight data but user had a cell in edit mode")
Else
Debug.WriteLine("system weight - refreshing data")
sqldataadapter.Fill(ds, mydata.Table.TableName)
dgvSystemWeightConfig.Focus()
UpdateLastUpdateTime()
End If
Catch ex As Exception
ExceptionHandler(ex)
Finally
enableUpdateTimer()
End Try
End Sub
【问题讨论】:
-
如果您投了反对票,您介意发表评论,让我知道您认为缺少什么或不正确吗?我总是尝试提供尽可能多的问题上下文和源代码,以帮助其他人了解我正在经历的事情,您的 cmets 将帮助我在这个过程中做得更好。
标签: .net vb.net winforms datagridview sqldataadapter