Click here to Skip to main content
15,916,180 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
Posted
Updated 20-Oct-10 3:06am
v2

1 solution

Try this:
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
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900