Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

VB.net DataGridView refresh data

Posted on 2011-09-14
5
Medium Priority
?
531 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 2000 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
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. …
Suggested Courses

610 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