Link to home
Start Free TrialLog in
Avatar of Murray Brown
Murray BrownFlag for United Kingdom of Great Britain and Northern Ireland

asked on

VB.net DataGridView not clearing

Hi

I am using the following VB.net code to bind data to a DataGridView.
When I add additional data to my table I call this procedure again but it
doesn't clear the data but rather just adds to the list. How do I get the data in the grid to
clear then populate so it shows what is actually in the table.

Thanks
Sub Fill_DGV() '*
        Dim dbadp As OleDbDataAdapter '*
        Dim dTable As New DataTable '*
        Try '*

            Me.DataGridView1.DataSource = Nothing

            Dim sSQL As String '*

            sSQL = "SELECT * FROM [Bookings]" '*
            Dim connection As New OleDbConnection(ConnectionString) '*
            dbadp = New OleDbDataAdapter(sSQL, connection) '*

            dbadp.Fill(dTable) '*
            Me.DataGridView1.DataSource = dTable '*

        Catch ex As Exception '*
            MsgBox(ex.Message) '*
        End Try '*
    End Sub

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Pratima
Pratima
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
or this
Sub Fill_DGV() '*
        Dim dbadp As OleDbDataAdapter '*
        Dim dTable As New DataTable '*
        Try '*

            Me.DataGridView1.DataSource = dTable

            Dim sSQL As String '*

            sSQL = "SELECT * FROM [Bookings]" '*
            Dim connection As New OleDbConnection(ConnectionString) '*
            dbadp = New OleDbDataAdapter(sSQL, connection) '*

            dbadp.Fill(dTable) '*
            Me.DataGridView1.DataSource = dTable '*

        Catch ex As Exception '*
            MsgBox(ex.Message) '*
        End Try '*
    End Sub

Open in new window

Avatar of Murray Brown

ASKER

Hi

None of this worked. I even tried the following to no avail:
 Me.DataGridView1.DataSource = Nothing
            Me.DataGridView1.Rows.Clear()
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi

dTable.Clear()

did the trick. there were duplicate rows, but all works well now
You probably did not have

>Dim dTable As New DataTable

because that would have done the trick anyway.
Thank you very much