VB2010在datagrid中读取csv,在网格中更新并保存到相同的csv

时间:2014-10-02 12:49:56

标签: vb.net csv datagridview

在VB2010中创建一个过程来读取datagridviewer中的csv文件,更新网格中的单元格并保存到相同的csvfile

在datagridviewer中打开csvfile工作正常, datagridviewer中的更新也可以正常工作

但是当我将datagrid保存到具有相同名称的csv文件时,我收到一条错误消息“进程无法访问该文件,因为它被其他进程使用”。

但如果我把它保存到其他文件名就可以了。 然后我尝试使用新文件名将新文件复制回原始文件名。 我收到了仍然无法复制的错误消息,因为该文件仍在使用中。

现在有人如何解决这个问题。

 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim fName As String = ""
        OpenFileDialog1.InitialDirectory = "H:\Data\2014\Software\VB2010\"
        OpenFileDialog1.Filter = "CSV files (*.csv)|*.CSV"
        OpenFileDialog1.FilterIndex = 2
        OpenFileDialog1.RestoreDirectory = True

        If (OpenFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK) Then
            fName = OpenFileDialog1.FileName
        End If

        Me.TextBox1.Text = fName

        Dim TextLine As String = ""
        Dim SplitLine() As String
        DataGridView1.ColumnCount = 5
        DataGridView1.Columns(0).Name = "Name"
        DataGridView1.Columns(1).Name = "Gender"
        DataGridView1.Columns(2).Name = "Age"
        DataGridView1.Columns(3).Name = "Ranking"
        DataGridView1.Columns(4).Name = "Date"

        If System.IO.File.Exists(fName) = True Then
            Dim objReader As New System.IO.StreamReader(fName)
            Do While objReader.Peek() <> -1
                TextLine = objReader.ReadLine()
                SplitLine = Split(TextLine, ",")
                Me.DataGridView1.Rows.Add(SplitLine)
            Loop
        Else
            MsgBox("File Does Not Exist")
        End If

    End Sub


    Private Sub SaveGridDataInFile(ByRef fName As String)
        'method called by button2
        Dim I As Integer = 0
        Dim j As Integer = 0
        Dim cellvalue$
        Dim rowLine As String = ""

        Try
            Dim objWriter As New System.IO.StreamWriter(fName, True)
            For j = 0 To (DataGridView1.Rows.Count - 2)
                For I = 0 To (DataGridView1.Columns.Count - 1)
                    If Not TypeOf DataGridView1.CurrentRow.Cells.Item(I).Value Is DBNull Then
                        cellvalue = DataGridView1.Item(I, j).Value
                    Else
                        cellvalue = ""
                    End If
                    rowLine = rowLine + cellvalue + ","
                Next
                objWriter.WriteLine(rowLine)
                rowLine = ""
            Next
            objWriter.Close()
            objWriter = Nothing
            MsgBox("Text written to file")
        Catch e As Exception
            MessageBox.Show("Error occured while writing to the file." + e.ToString())
        Finally
            FileClose(1)
        End Try
        Call copy_file()
    End Sub


    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
          
                'çall method SaveGridDataInFile
                SaveGridDataInFile(Me.TextBox1.Text)
                '  FileCopy("H:\Data\2014\Software\VB2010\datagr_ex2.csv", "H:\Data\2014\Software\VB2010\datagr_ex.csv")
    End Sub
Sub copy_file()
        Dim FileToDelete As String

        FileToDelete = "H:\Data\2014\Software\VB2010\datagr_ex.csv"

        If System.IO.File.Exists(FileToDelete) = True Then

            System.IO.File.Delete(FileToDelete)
            MsgBox("File Deleted")

        End If

        FileCopy("H:\Data\2014\Software\VB2010\datagr_ex2.csv", "H:\Data\2014\Software\VB2010\datagr_ex.csv")
    End Sub

1 个答案:

答案 0 :(得分:1)

我猜您需要关闭用于加载文件的StreamReader,然后才能重新打开文件进行保存。 StreamReader类实现IDisposable,因此您可以使用VB.Net的Using Statement在读完后自动关闭文件,即

    If System.IO.File.Exists(fName) = True Then
        Using objReader As New System.IO.StreamReader(fName)
            Do While objReader.Peek() <> -1
                TextLine = objReader.ReadLine()
                SplitLine = Split(TextLine, ",")
                Me.DataGridView1.Rows.Add(SplitLine)
            Loop
        End Using
    Else
        MsgBox("File Does Not Exist")
    End If