Solved

VB.net DataGridView refresh data

Posted on 2011-09-14
5
512 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:Murray Brown
[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:Murray Brown
ID: 36546279
thanks very much
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
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 …
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

688 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