簡體   English   中英

vb.net bindingSource和bindingNavigation以編程方式

[英]vb.net bindingSource and bindingNavigation programmatically

請查看以下代碼,並告訴我為什么它不移至下一條記錄? 我以編程方式加載數據並在數據集中填充表格。 我可以通過向導來完成,但是我想用自己的代碼來完成。 因為使用向導有時無助於理解其背后的真實代碼。

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

我找到了解決方案。 數據邊界應如下所示:

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")

但是沒關系,我應該將上面代碼行中的dsOptions.tables(“ institute”)替換為我像下面這樣理解的bindingSource

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

這樣,我可以使用bindingSource對象瀏覽數據集中的記錄。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM