Solved

VB.net DataGridView refresh data

Posted on 2011-09-14
5
499 Views
Last Modified: 2012-05-12
Hi

I have a data bound DataGridView which I populate with the following code.
I included the following three lines to ensure that any changes made are reflected visually
but this is not happening. Have I got the code or the order wrong
    Me.Supplier_DataGridView.EndEdit()
            Me.Supplier_DataGridView.DataSource = Nothing
            Me.Supplier_DataGridView.Refresh()

Sub Fill_Supplier_DGV()
        Try

            Me.Supplier_DataGridView.EndEdit()
            Me.Supplier_DataGridView.DataSource = Nothing
           


            Dim sSQL As String

            sSQL = "SELECT * FROM Suppliers"

            Dim connection As New OleDbConnection(ConnectionString)
            Dim dataadapter As New OleDbDataAdapter(sSQL, connection)
            Dim ds As New DataSet()
            connection.Open()
            dataadapter.Fill(ds, "Suppliers_table")
            connection.Close()

            Me.Supplier_DataGridView.DataSource = ds
            Me.Supplier_DataGridView.DataMember = "Suppliers_table"
 Me.Supplier_DataGridView.Refresh()

        Catch ex As Exception
            MsgBox(ex.Message)
        End Try

    End Sub

Open in new window

0
Comment
Question by:murbro
5 Comments
 
LVL 17

Accepted Solution

by:
nepaluz earned 500 total points
ID: 36535436
Can't see anything wrong with that, try removing this line:
Me.Supplier_DataGridView.EndEdit()

Open in new window

0
 
LVL 40
ID: 36536345
Since you are working with only one table, you do not need a DataSet. You simply need DataTable.
0
 
LVL 17

Expert Comment

by:nepaluz
ID: 36536464
That last comment just jogged me into suggesting you change the datasource line to:
Me.Supplier_DataGridView.DataSource = ds.tables("Suppliers_table")

Open in new window

0
 
LVL 15

Expert Comment

by:x77
ID: 36538303
I do´nt see any error on your code.
I Test it and it Works Fine.

Note that Refresh and EndEdit lines are innecesary on this context:

  You Do EndEdit to make edited Control save changes to DataSet.
  But you do´n Save Changes to DataTable.

  Then you Remove DataSource from DataGridView.
  At This point, Datasource contains Modified Data, But it is not Referenced.

  Now You Create a new DataSet , Fill it with Data and Bind to DataGridView.
  Now your DataGridview is Sync with Query - But your changes are lost.

To Update DataTable before requery Table, you need:

    Verify that there are any change : Ds.HasChanges
       Note that you need Get Dataset from DataSource : ds = Directcast(DataGridview,DataSet)

   Now when DataAdapter is created:
         If Ds.HasChanges then
             Dim Cb = New OleDbCommandBuilder(dataadapter ) 'This Builds the Insert,Update and Delete Sentences
             dataadapter.Update(Ds, "Suppliers_table")




         end if
   
0
 

Author Closing Comment

by:murbro
ID: 36546279
thanks very much
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Encryption Decryption in Oracle 12 105
Replacing HTML tags in Textarea/Textbox 5 47
VB.Net - CSV to Oracle table 4 52
Get Client IP on RDS - VB.NET 15 25
Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

911 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now