我好像有问题。我有一个dataset
(excel)来源于datagridview
一个 Windows 窗体应用程序。我希望在对单元格进行更改后找到一种方法来刷新/更新我dataset
的内容。datagridview
让我来看看我为尝试实现这一目标所采取的步骤。我button
在表单中添加了“刷新”,并创建了一个名为的方法,该方法write2DGV
将dataset
在datagridview
.
我添加了一个计时器,它允许 Excel 更新以更新输出。我还添加了代码来清除我的dataset
(这是 的数据源datagridview
)。最后我有一个检索方法,它将dataset
通过datagridview
.oledataadapter
不幸的是,当我运行代码时,我收到一条错误消息,提示您没有提供密钥作为 get update 方法的参数。我不确定它们所说的“键”是什么意思,get update 方法的参数唯一有效的数据类型是布尔值。我的代码如下:
'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