Click here to Skip to main content
15,885,890 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello I seem to be having an issue. I have a dataset (excel) sourced to a datagridview in a windows forms application. I'm hoping to find a way to refresh/update my dataset within the datagridview after making a change to a cell. Let me go through the steps I've taken to try and accomplish this. I've added a "refresh" button to the form and I've created a method called write2DGV which will write changes to the dataset after changes are made in the datagridview. I've added a timer that will allow Excel to update to update the outputs. I've also added code to wipe clean my dataset (which is the datasource for the datagridview). Last I have a retrieve method that will source the dataset to the datagridview via the oledataadapter. Unfortunately I've had no luck getting my refresh to update the changes within the datagridview. My code is below:
VB
 'Declarations
   Dim myDataSet As DataSet
Dim MyCommand As OleDb.OleDbDataAdapter
Dim objWorkSheet As Excel.Worksheet = objExcel.ActiveSheet
 Dim sizetimer As New System.Timers.Timer

     Sub retrieveMyDataSet()
        MyCommand = New OleDbDataAdapter(select * from [MyExcelWorksheet$A13:x150], MyConnection)

        myDataSet = New System.Data.DataSet()
        MyCommand.Fill(myDataSet)
        myDataGridView.DataSource = myDataSet.Tables(0).DefaultView
    End Sub

Sub write2Size()

        A-k gets written.  Entire graph goes to a-x so I only need to writ the columncount -14
       x is letter 24. k is letter 11.  24-11 = 13.  So, offset needs to be -13
     39;Rows 13 through first blank
            Dim rowindex As Integer
            Dim columnindex As Integer
            For rowindex = 1 To myDataGridView.RowCount
            For columnindex = 1 To myDataGridView.ColumnCount - 13
                objWorkSheet.Cells(rowindex + 13, columnindex + 0) = myDataGridView(columnindex     - 1, rowindex - 1).Value
            Next
        Next
    End Sub

Sub refreshDGV()


       write2DGV()
        myDataSet.Clear()
        Dim x As New OleDbDataAdapter("select * from [MyExcelWorksheet$A13:x150]", MyConnection)
        Dim oledbCommands As New OleDb.OleDbCommandBuilder(x)
        x.UpdateCommand = oledbCommands.GetUpdateCommand() 'oops type! removed = x.UpdateCommand
        x.Update(myDataSet)
        sizetimer.AutoReset = True
        sizetimer.Interval = 2000 '2 seconds
        retrieveMyDataSet()


   End Sub

Private Sub refreshbtn_Click(sender As System.Object, e As System.EventArgs) Handles refreshbtnSize.Click
       refreshDGV()


   End Sub
Posted
Updated 22-Aug-13 12:43pm
v4

You will need something like below. You can create a new datadapter and everything when you go to update the data (from my experience anyways). You will need the dataset and the row states for the dataset should be set to trigger updates for those modified rows. If you accept changes on your dataset then those are lost and nothing in your external source will be updated if I remember correctly.

VB
Dim x As New OleDb.OleDbDataAdapter("select * from somewhere", myconnection (OleDb.OleDbConnection))
       Dim oledbCommands As New OleDb.OleDbCommandBuilder(x)
       x.UpdateCommand = oledbCommands.GetUpdateCommand()'oops type! removed = x.UpdateCommand

       'update the adapter
       x.Update(DataSet)


Addition:
Maybe I misunderstood your goal. Are you just trying to get the DataGridView to refresh its contents when you update the datasource? If so you should be able to add a line
VB
myDataGridView.DataSource = myDataSet.Tables(0).DefaultView
myDataGridView.Refresh 'should redraw with the new data
 
Share this answer
 
v3
Comments
CAS1224 22-Aug-13 18:46pm    
Thanks for your answer. I've added your code to my refresh method but I'm getting the following error: "Dynamic SQL generation for the UpdateCommand is not supported against a SelectCommand that does not return any key column information." which is referring to the x.UpdateCommand = oledbCommands.GetUpdateCommand(). Yes I'm trying to update the dataset and refresh however the myDataGridView.Refresh() doesn't work for my program. (Nothing is ever that easy haha)
CAS1224 22-Aug-13 18:47pm    
Check out my refresh method, I've added code
Trak4Net 23-Aug-13 3:36am    
So the update command is only used to push modified data back to the original datasource SQL etc. in your case it is excel. I found this link if that is one of the things you are trying to accomplish. http://blogs.msdn.com/b/spike/archive/2008/10/29/how-to-update-an-excel-worksheet-using-dataset-and-the-oledbdataadapter.aspx
For refreshing the datagridview I found this blog that discusses the issues with .Refresh
http://social.msdn.microsoft.com/Forums/windows/en-US/18a9762e-ac67-48a7-a372-55307fe344f3/how-do-you-refresh-data-bound-to-a-datagridview
Hope that helps you out.
CAS1224 23-Aug-13 8:34am    
Thanks so much for your help!
CAS1224 23-Aug-13 8:40am    
Yes I'm trying to update the datagridview with new data. I checked out the second post, one of them mentioned filling the datagridview. This is what they used "this.rscan_queueTableAdapter.Fill(this.rscanDataSet.rscan_queue);
dgv_queue.refresh();" Is there a way to do something similar with the oledbdataadapter?
Always set the tableadapters ClearBeforeFill property to true
then refill data set by issuing tableadapter.fill(dataset.tablename)

A sample from one of the my code
If Not IsNothing(dgvCountries.CurrentRow) Then
Dim frm As New frmEditCountry
frm.CountryID = dgvCountries.CurrentRow.Cells(0).Value
frm.EditRecord = True
frm.ShowDialog()

Me.CountriesTableAdapter.Fill(Me.DSLMS.Countries)
dgvCountries.Refresh()
End If
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900