本文介绍了BindingNavigator和DataGridView无法连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在表单上有一个dataviewgrid,一个绑定源,一个表适配器和一个绑定导航器.
我使用以下内容填充datagridview和绑定导航器.它可以工作,但是bindingnavigator和dataviewgrid不链接.任何人都可以提供答案吗?

I have a dataviewgrid, a bindingsource, a tableadaptor and a bindingnavigator on a form.
I use the following to populate both the datagridview and binding navigator. It works but the bindingnavigator and the dataviewgrid do not link. Can anyone please provide an answer?

Public Sub FillGrid(ByVal sQuery As String, ByVal sTable As String)
ConnectionString = My.Settings.ABCConnectionString
Dim ds As New DataSet()
Dim dataadapter As New OleDb.OleDbDataAdapter(sQuery, Connection)
dataadapter.Fill(ds, sTable)
BindingSource.DataSource = ds
BindingSource.DataMember = sTable
BindingSource.ResetBindings(False)
datagridview.DataSource = BindingSource.DataSource
datagridview.DataMember = sTable
BindingNavigator.BindingSource = BindingSource
BindingNavigator.Update()
END SUB

推荐答案

Me.datagridview.DataSource = Me.BindingSource



我已经在以下代码上对其进行了测试:



I have tested it on this code:

Private Sub FillGrid()
    Dim oDt As DataTable = Nothing, oRow As DataRow = Nothing, oCol As DataColumn = Nothing
    Dim i As Integer = 0, j As Integer = 0, bindsrc As BindingSource = Nothing

    Try
        ''create new datatable
        oDt = New DataTable("SomeData")

        ''add columns
        For i = 0 To 4
            oCol = New DataColumn(CStr("Col_" & i + 1), GetType(Integer))
            oDt.Columns.Add(oCol)
        Next

        ''add data (rows)
        For i = 0 To 9
            oRow = oDt.NewRow()
            For j = 0 To 4
                ''fill columns
                oRow.Item(CStr("Col_" & j + 1)) = (j + 1) * (i + 1)
            Next
            oDt.Rows.Add(oRow)
        Next

        ''create new binding source
        bindsrc = New BindingSource()
        bindsrc.DataSource = oDt
        ''bind BindingSource
        Me.BindingNavigator1.BindingSource = bindsrc
        ''bind DGV to BindingSource (BindingNavigator)
        Me.DataGridView1.DataSource = Me.BindingNavigator1.BindingSource

    Catch ex As Exception
        MsgBox(ex.Message, MsgBoxStyle.Exclamation, "Error!")

    Finally
        oCol = Nothing
        oRow = Nothing
        oDt = Nothing

    End Try
End Sub


这篇关于BindingNavigator和DataGridView无法连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-18 23:57