[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

VB.net DataGridView not clearing

Posted on 2011-10-18
7
Medium Priority
?
231 Views
Last Modified: 2012-05-12
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

0
Comment
Question by:Murray Brown
  • 3
  • 2
  • 2
7 Comments
 
LVL 39

Accepted Solution

by:
Pratima Pharande earned 1000 total points
ID: 36984394
try this
Sub Fill_DGV() '*
        Dim dbadp As OleDbDataAdapter '*
        Dim dTable As New DataTable '*
        Try '*
dTable = Nothing
            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

0
 
LVL 39

Expert Comment

by:Pratima Pharande
ID: 36984396
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

0
 

Author Comment

by:Murray Brown
ID: 36986006
Hi

None of this worked. I even tried the following to no avail:
 Me.DataGridView1.DataSource = Nothing
            Me.DataGridView1.Rows.Clear()
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 83

Assisted Solution

by:CodeCruiser
CodeCruiser earned 1000 total points
ID: 36988250
Are you sure that even with

Dim dTable As New DataTable

you are getting duplicate rows? Show us the code where you are calling the fill function. Also a screenshot of duplicates if possible.
0
 

Author Comment

by:Murray Brown
ID: 36988391
Hi

dTable.Clear()

did the trick. there were duplicate rows, but all works well now
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 36991407
You probably did not have

>Dim dTable As New DataTable

because that would have done the trick anyway.
0
 

Author Closing Comment

by:Murray Brown
ID: 36994389
Thank you very much
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses
Course of the Month17 days, 16 hours left to enroll

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question