0

请看下面的代码并告诉我为什么它没有移动到下一条记录?我以编程方式加载数据并在数据集中填写表格。我可以通过向导做到这一点,但我想用我自己的代码做到这一点;因为使用向导有时无助于理解其背后的真实代码。

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
4

1 回答 1

0

我找到了解决方案。数据绑定应如下所示:

InstIdTextBox.DataBindings.Add("Text", bsInstitute, "instId") CodeTextBox.DataBindings.Add("Text", bsInstitute, "code") NameTextBox.DataBindings.Add("Text", bsInstitute, "name") TypeTextBox.DataBindings.Add("Text", bsInstitute, "type") 
dataset instead of bsInstitute. But now its perfect.    Try
            dsOptions = New DataSet
            loadOptions()

            bsInstitute = New BindingSource(dsOptions, "institute")

            InstIdTextBox.DataBindings.Add("Text", bsInstitute, "instId")
            CodeTextBox.DataBindings.Add("Text", bsInstitute, "code")
            NameTextBox.DataBindings.Add("Text", bsInstitute, "name")
            TypeTextBox.DataBindings.Add("Text", bsInstitute, "type")

        Catch ex As Exception
            MsgBox(Err.Description)
        End Try

我正在使用数据集进行这样的绑定

InstIdTextBox.DataBindings.Add("Text", dsOptions.Tables("institute"),"instId")

但是我应该用我创建的 bindingSource 替换上面代码行中的 dsOptions.tables("institute") ,如下所示

InstIdTextBox.DataBindings.Add("Text", bsInstitute, "instId")

这样我就可以使用 bindingSource 对象来导航我的数据集中的记录。

于 2013-11-16T07:06:31.530 回答