如何刷新/更新vb.net中的数据集以获取Windows窗体 [英] How to refresh/update dataset in vb.net for windows forms

查看:384
本文介绍了如何刷新/更新vb.net中的数据集以获取Windows窗体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好我好像有问题。我有一个数据集(excel)来源于Windows窗体应用程序中的datagridview。我希望在对单元格进行更改后,在datagridview中找到一种刷新/更新数据集的方法。让我通过我试图完成的步骤。我在表单中添加了一个刷新按钮,我创建了一个名为write2DGV的方法,该方法将在datagridview中进行更改后将更改写入数据集。我添加了一个计时器,允许Excel更新以更新输出。我还添加了代码来擦除我的数据集(这是datagridview的数据源)。最后我有一个检索方法,它将通过oledataadapter将数据集提供给datagridview。不幸的是,我没有幸运获得刷新来更新datagridview中的更改。我的代码如下:

 ' 声明 
Dim myDataSet As DataSet
Dim MyCommand As OleDb.OleDbDataAdapter
Dim objWorkSheet As Excel.Worksheet = objExcel.ActiveSheet
Dim sizetimer As System.Timers.Timer

Sub retrieveMyDataSet()
MyCommand = OleDbDataAdapter(选择 *来自[MyExcelWorksheet $ A13:x150],MyConnection)

myDataSet = System.Data.DataSet()
MyCommand.Fill(myDataSet)
myDataGridView.DataSource = myDataSet.Tables( 0 )。DefaultView
结束 Sub

Sub write2Size()

Ak写入。整个图表 ax所以我只需要 写入columncount -14
x 字母 24 。 k 字母 11 。 24-11 = 13 。因此,抵消需要 为-13
39;行 13 至第一个空白
Dim rowindex 作为 整数
Dim columnindex 作为 整数
对于 rowindex = 1 myDataGridView。 RowCount
对于 columnindex = 1 To myDataGridView.ColumnCount - 13
objWorkSheet.Cells(rowindex + 13 ,columnindex + < span class =code-digit> 0 )= myDataGridView(columnindex - 1 ,rowind ex - 1 )。值
下一步
下一步
结束 Sub

Sub refreshDGV()


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


结束 Sub

私有 Sub refreshbtn_Click(发件人 As System。 Object ,e As System.EventArgs) Handles refreshbtnSize.Click
refreshDGV()


结束 Sub

解决方案

A13:x150],MyConnection)

myDataSet = System.Data.DataSet()
MyCommand.Fill(myDataSet)
myDataGridView.DataSource = myDataSet.Tables( 0 )。DefaultView
结束 Sub

Sub write2Size()

Ak写入。整个图表 ax所以我只需要 写入columncount -14
x 字母 24 。 k 字母 11 。 24-11 = 13 。因此,抵消需要 为-13
39;行 13 至第一个空白
Dim rowindex 作为 整数
Dim columnindex 作为 整数
对于 rowindex = 1 myDataGridView。 RowCount
对于 columnindex = 1 To myDataGridView.ColumnCount - 13
objWorkSheet.Cells(rowindex + 13 ,columnindex + < span class =code-digit> 0 )= myDataGridView(columnindex - 1 ,rowind ex - 1 )。值
下一步
下一步
结束 Sub

Sub refreshDGV()


write2DGV()
myDataSet.Clear()
Dim x As OleDbDataAdapter( select [from [MyExcelWorksheet


A13:x150],MyConnection)
Dim oledbCommands 作为 OleDb.OleDbCommandBuilder (x)
x.UpdateCommand = oledbCommands.GetUpdateCommand()' oops type! removed = x.UpdateCommand
x.Update(myDataSet)
sizetimer.AutoReset = True
sizetimer.Interval = 2000 ' 2秒
retrieveMyDataSet()


结束 Sub

私有 Sub refreshbtn_Click(发件人 As System。 Object ,e As System.EventArgs) Handles refreshbtnSize.Click
refreshDGV()


结束 Sub


您需要以下内容。您可以在更新数据时创建新的datadapter以及所有内容(根据我的经验)。您将需要数据集,并且应将数据集的行状态设置为触发那些已修改行的更新。如果您接受对数据集的更改,那么这些更改将会丢失,如果我没记错,外部源中的任何内容都不会更新。



 < span class =code-keyword> Dim  x  As   OleDb.OleDbDataAdapter(< span class =code-string>  select * from somewhere,myconnection(OleDb.OleDbConnection))
Dim oledbCommands 作为 OleDb.OleDbCommandBuilder(x)
x.UpdateCommand = oledbCommands.GetUpdateCommand()' oops类型! removed = x.UpdateCommand

' 更新适配器
x.Update(DataSet)





另外:

也许我误解了你的目标。您是否只是在更新数据源时尝试让DataGridView刷新其内容?如果是这样你应该能够添加一行

 myDataGridView.DataSource = myDataSet.Tables( 0 ) .DefaultView 
myDataGridView.Refresh ' 应重新绘制新数据


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:

 '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

解决方案

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


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.

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

myDataGridView.DataSource = myDataSet.Tables(0).DefaultView
myDataGridView.Refresh 'should redraw with the new data


这篇关于如何刷新/更新vb.net中的数据集以获取Windows窗体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆