Excel 关闭应用程序后,从datagridview检索行/数据

Excel 关闭应用程序后,从datagridview检索行/数据,excel,vb.net,datagridview,devexpress,oledb,Excel,Vb.net,Datagridview,Devexpress,Oledb,我这里有一个对话框表单,用户可以从Excel上传一个文件,然后导入并显示到datagrid视图。但是,我的问题是,当用户关闭应用程序或表单时,datagridview为空并重置 我的问题是如何在每次用户打开表单时检索并显示它的数据。如果数据未保存在数据库中且仅基于上载或导入的Excel文件,是否可能?如果用户上传了新的Excel,我想替换datagridview中现有的行或数据 我真的希望你能帮忙 您可以在下面找到我的代码: Imports System.Data Public Class fr

我这里有一个对话框表单,用户可以从Excel上传一个文件,然后导入并显示到datagrid视图。但是,我的问题是,当用户关闭应用程序或表单时,datagridview为空并重置

我的问题是如何在每次用户打开表单时检索并显示它的数据。如果数据未保存在数据库中且仅基于上载或导入的Excel文件,是否可能?如果用户上传了新的Excel,我想替换datagridview中现有的行或数据

我真的希望你能帮忙

您可以在下面找到我的代码:

Imports System.Data
Public Class frmSupplier


Private Sub btn_upload_supplier_Click(sender As Object, e As EventArgs) Handles btn_upload_supplier.Click
        Try
            If btn_upload_supplier.Text = "New" Then
                Dim Openfile As New OpenFileDialog
                Openfile.ShowDialog()
                txtpath_supplier.Text = Openfile.FileName
                Label1.Text = txtpath_supplier.Text
                If txtpath_supplier.Text = Nothing Then
                Else
                    btn_upload_supplier.Text = "Upload"
                End If
            ElseIf btn_upload_supplier.Text = "Upload" Then
                Dim dtItemlist As New DataTable
                Dim obj As New Basegeneral

                Try
                    Dim MyConnection As System.Data.OleDb.OleDbConnection
                    Dim DtSet As System.Data.DataSet
                    Dim MyCommand As System.Data.OleDb.OleDbDataAdapter
                    MyConnection = New System.Data.OleDb.OleDbConnection("provider=Microsoft.Jet.OLEDB.4.0;  Data Source=" + txtpath_supplier.Text.ToString() + "; Extended Properties=Excel 8.0;")
                    MyCommand = New System.Data.OleDb.OleDbDataAdapter("select * from [Sheet1$]", MyConnection)
                    MyCommand.TableMappings.Add("Table", "TestTable")
                    DtSet = New System.Data.DataSet
                    MyCommand.Fill(DtSet)
                    dg_details.DataSource = DtSet.Tables(0)

                    MyConnection.Close()

                Catch ex As Exception
                    MessageBox.Show(Me, ex.ToString, "Error:")
                End Try

            End If

        Catch ex As Exception
            MessageBox.Show(Me, ex.ToString, "Error:")
        End Try

    End Sub




    Private Sub btnDelete_Click(sender As Object, e As EventArgs) Handles btnDelete.Click
        Try
            Dim row As System.Data.DataRow = GridView1.GetDataRow(GridView1.FocusedRowHandle)

            If row.Item(0).ToString = "" Then
                MessageBox.Show("Please select supplier to remove", "KCC Retail System")
                Return
            End If

            If DevExpress.XtraEditors.XtraMessageBox.Show("Are you sure you want to remove supplier  '" & row.Item(0) & "'?", "WARNING", MessageBoxButtons.YesNo, MessageBoxIcon.Error, MessageBoxDefaultButton.Button2) = Windows.Forms.DialogResult.Yes Then
                MessageBox.Show("Supplier  " & row.Item(0) & "  has been removed!", "KCC Retail System")
                GridView1.DeleteRow(GridView1.FocusedRowHandle)
            Else
                Return
            End If


        Catch ex As Exception
            MessageBox.Show(ex.Message, "Error :")
        End Try

    End Sub

    Private Sub btn_clear_Click(sender As Object, e As EventArgs) Handles btn_clear.Click
        btn_upload_supplier.Text = "New"
        txtpath_supplier.Text = ""
    End Sub

当用户单击按钮时,表单将打开,并显示为对话框(ShowDialog)。

要保存DataGridView的内容,然后重新添加内容,可以使用二进制格式化程序

Imports System.IO
Import System.Runtime.Serialization.Formatters.Binary
    Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
        Dim FileName As String = "MyData.dat" 'Will save in bin directory
        Dim dt As DataTable = CType(DataGridView1.DataSource, DataTable)
        ' 1. Create an instance of a Binary Formatter
        Dim bf As New BinaryFormatter
        ' 2. Create a file stream passing in (the name of the file to create, mode: Create, access: Write, share: none)
        Using fs As Stream = New FileStream(FileName, FileMode.Create, FileAccess.Write, FileShare.None)
            ' 3. use the Serialize method of the Binary Formatter passing in the file stream(file name, create, write, no share) And the object
            bf.Serialize(fs, dt)
        End Using
    End Sub

    Private Sub btnFillFromFill_Click(sender As Object, e As EventArgs) Handles btnFillFromFill.Click
        Dim FileName As String = "MyData.dat"
        Dim dt As DataTable
        ' 1. Create an instance of Binary Formatter
        Dim bf As New BinaryFormatter()
        ' 2. Get an instance of a FileStream for the OpenRead method of File passing in (the fileName)
        Using fs As Stream = File.OpenRead(FileName)
            ' 3. cast back to original object, the Deserialize method of the Binary Formatter passing in the File Stream
            dt = CType(bf.Deserialize(fs), DataTable)
        End Using
        DataGridView1.DataSource = dt
    End Sub 

你能举例说明你所拥有的以及你正在尝试做的事情吗?这些标签是随机选择的还是以某种方式应用的?@ashleedawg,是的,它以某种方式应用,因为我使用dev express grid view、oledb和ms excelPlease请不要将DataAdapter命名为MyCommand。ADO.net中有一个命令对象,它使您的代码变得混乱。@Mary,很抱歉,我只是从internet上复制了它。