Solved

VB.net DataGridView refresh data

Posted on 2011-09-14
5
508 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

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!

Question has a verified solution.

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

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
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…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

749 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