Solved

VB.net DataGridView refresh data

Posted on 2011-09-14
5
498 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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

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…
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

707 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