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

时间:2018-06-05 10:10:49

标签: excel vb.net datagridview devexpress oledb

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

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

我真的希望你会帮忙。

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

 else {
   //errorresponse from restcall to thirdparty-api (just removing prefix here)
   responseString = responseString.replace("sap_error_", "");

   //actually I try to do a somewhat silent idplogin, so this might fit. 
   //you can change this error to the errormsg from thirdparty, too,
   // so its collected in the logs error=-part.
   context.getEvent().error(Errors.IDENTITY_PROVIDER_ERROR);

   //here we're building the actual responseHandler.
   //One might even want to set the status to status from thirdparty. 
   Response challengeResponse = context.form().setError(responseString)
                .createErrorPage(Response.Status.INTERNAL_SERVER_ERROR);

   //And in the end we apply the failureChallenge to the Context  
   context.failureChallenge(AuthenticationFlowError.IDENTITY_PROVIDER_ERROR,
                                     challengeResponse);
}

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

1 个答案:

答案 0 :(得分:0)

要保存并重新保存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