如何刷新/更新datagridview窗体 [英] How to refresh/update datagridview windows form

查看:133
本文介绍了如何刷新/更新datagridview窗体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法找到用于刷新datagridview的解决方案。我的目标是有一个刷新按钮,它会在进行更改并按下刷新按钮后自动更新数据库(excel工作表)。我的程序响应方式:我对datagridview进行了更改并按下了我的刷新按钮,程序加载然后恢复为原始数据。这是我的代码到目前为止的样子。





I'm having trouble finding a solution for refreshing my datagridview. My goal is to have a refresh button that will automatically update the database(excel worksheet) after making a change and pressing the refresh button. The way my program responds: I make a change to the datagridview and press my refresh button, the program loads and then reverts back to the original data. Here is how my code looks so far.


MyCommand1 = New OleDbDataAdapter("select * from [MyExcel$A13:x150]", MyConnection)

       DataSet1 = New System.Data.DataSet()
       MyCommand1.Fill(DataSet)


       'This displays the data to the GUI but does not save it to the DB table that we ultimately want it saved to
       DataGridView1.DataSource = DataSet1.Tables(0).DefaultView
















 Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
            Try
                oledbCmdBuilder = New OleDbCommandBuilder(oledbAdapter)
                changes = DataSet1.GetChanges()
                If changes IsNot Nothing Then
                oledbAdapter.Update(DataSet1.Tables(0))
            End If
            DataSet.AcceptChanges()
            MsgBox("Save changes")
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
    End Sub
End Class

推荐答案

A13:x150],MyConnection)

DataSet1 = System.Data.DataSet()
MyCommand1.Fill(DataSet)


' 这会向GUI显示数据,但不会将其保存到我们最终希望保存到的数据库表中
DataGridView1.DataSource = DataSet1.Tables( 0 )。DefaultView
A13:x150]", MyConnection) DataSet1 = New System.Data.DataSet() MyCommand1.Fill(DataSet) 'This displays the data to the GUI but does not save it to the DB table that we ultimately want it saved to DataGridView1.DataSource = DataSet1.Tables(0).DefaultView
















 Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
            Try
                oledbCmdBuilder = New OleDbCommandBuilder(oledbAdapter)
                changes = DataSet1.GetChanges()
                If changes IsNot Nothing Then
                oledbAdapter.Update(DataSet1.Tables(0))
            End If
            DataSet.AcceptChanges()
            MsgBox("Save changes")
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
    End Sub
End Class


你可以在刷新按钮中调用表单构造函数

Form1_Load(sender,e);
you may call the form constructor in refresh button
Form1_Load(sender, e);


这篇关于如何刷新/更新datagridview窗体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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