【发布时间】:2013-11-13 09:01:13
【问题描述】:
请看下面的代码并告诉我为什么它没有移动到下一条记录?我以编程方式加载数据并在数据集中填写表格。我可以通过向导做到这一点,但我想用我自己的代码做到这一点;因为使用向导有时无助于理解其背后的真实代码。
Private Sub frmSystemOptions_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Try
dsOptions = New DataSet
loadOptions()
bsInstitute = New BindingSource(dsOptions, "institute")
bnInstitute = New BindingNavigator(bsInstitute)
InstIdTextBox.DataBindings.Add("Text", dsOptions.Tables("institute"),"instId")
CodeTextBox.DataBindings.Add("Text", dsOptions.Tables("institute"), "code")
NameTextBox.DataBindings.Add("Text", dsOptions.Tables("institute"), "name")
TypeTextBox.DataBindings.Add("Text", dsOptions.Tables("institute"), "type")
Catch ex As Exception
MsgBox(Err.Description)
End Try
End Sub
Sub loadOptions()
Dim sql As String
Try
sqlConn = New SqlConnection(connString)
sqlConn.Open()
sql = "select * from institute"
daAdapter = New SqlDataAdapter(sql, sqlConn)
daAdapter.Fill(dsOptions, "institute")
'----------------------------------------------------------------------
sqlConn.Close()
Catch ex As Exception
sqlConn.Close()
MsgBox(Err.Description)
End Try
End Sub
Private Sub btnNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNext.Click
If bsInstitute.Position + 1 < bsInstitute.Count Then
bsInstitute.MoveNext()
Else
bsInstitute.MoveFirst()
End If
Me.Validate()
End Sub
【问题讨论】:
-
也许你的 DataBinding 是错误的。您移动了 BindingSource,但您已绑定到 DataSet。处理事件 bsInstitue_CurrentChanged (或类似的东西)并放入断点。你会看到它在移动。
-
我找到了解决方案。数据绑定应如下所示: InstIdTextBox.DataBindings.Add("Text", bsInstitute, "instId") CodeTextBox.DataBindings.Add("Text", bsInstitute, "code") NameTextBox.DataBindings.Add("Text", bsInstitute, "name") TypeTextBox.DataBindings.Add("Text", bsInstitute, "type") 在我的代码中,我使用的是数据集而不是 bsInstitute。但现在它完美了
标签: vb.net move next bindingsource bindingnavigator