locked
Read/Write CSV files with datagridview RRS feed

  • Question

  •  

    Hi,

     

    I'm developingn a small aplication that manages excel files and read/write information to those files. Well, i'm working with excel files because that's the only way i know.

     

    I think it would be better text files or CSV.

     

    My question is how can i read/write CSV files with a datagridview?

     

    Thanks

     

    Monday, May 19, 2008 5:44 PM

Answers

  • well if you need to write the column values for each row in the datagridview, then simply go through each row and each column and write the value to the file seperated by a comma.

     

    example:

     

    Dim builder as new StringBuilder()

     

    for each currentRow as DataGridViewRow in Me.theDataGridView.Rows

     

       for each currentCell as DataGridViewCell in currentRow.Cells

     

          builder.Append(currentCell.Value)

          builder.Append(",")

     

       next

     

       builder.Append(Environment.NewLine)

     

    next

     

    builder.Remove(builder.Length - 1, 1) 'remove the last comma

     

    using writer as new StreamWriter("myfile.csv")

       writer.Write(builder.ToString())

       writer.Close()

    end using

     

    does this help?

     

    Monday, May 19, 2008 7:04 PM

All replies

  • well if you need to write the column values for each row in the datagridview, then simply go through each row and each column and write the value to the file seperated by a comma.

     

    example:

     

    Dim builder as new StringBuilder()

     

    for each currentRow as DataGridViewRow in Me.theDataGridView.Rows

     

       for each currentCell as DataGridViewCell in currentRow.Cells

     

          builder.Append(currentCell.Value)

          builder.Append(",")

     

       next

     

       builder.Append(Environment.NewLine)

     

    next

     

    builder.Remove(builder.Length - 1, 1) 'remove the last comma

     

    using writer as new StreamWriter("myfile.csv")

       writer.Write(builder.ToString())

       writer.Close()

    end using

     

    does this help?

     

    Monday, May 19, 2008 7:04 PM
  • Hi ahmedilyas

     

    Yes, that does help. In fact that's what i need to write form a datagridview to a CSV file.

     

    Thanks a lot.

     

    Still i need to read form the CSV file into the datagridview...

    Wednesday, May 21, 2008 7:50 AM
  • Hi ahmedilyas

     

    When using your code hapens that it's written a row only with commas to the text file... wich is the last row that allways appears in datagridview when AllowUserToAddRows = True.

     

    Then, when opening the file, it's added two empty rows at the end of datagridview, one because it exists in the text file and the other because datagridview AllowUserToAddRows = True.

     

    So to avoid this extra line got to AllowUserToAddRows = False before write it to the file.

     

    Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click

    DataGridView1.AllowUserToAddRows = False

    Dim builder As New StringBuilder()

    For Each currentRow As DataGridViewRow In Me.DataGridView1.Rows

    For Each currentCell As DataGridViewCell In currentRow.Cells

    builder.Append(currentCell.Value)

    builder.Append(",")

    Next

    builder.Append(Environment.NewLine)

    Next

    builder.Remove(builder.Length - 1, 1) 'remove the last comma

    Using writer As New StreamWriter("E:\Os meus projectos\OF\001.txt")

    writer.Write(builder.ToString())

    writer.Close()

    End Using

    DataGridView1.AllowUserToAddRows = True

    End Sub

     

    To read the text file i´m using the code below that i've found here

     

    Using parser As New Microsoft.VisualBasic.FileIO.TextFieldParser("c:\temp\myFile.csv")
       parser.SetDelimiters(",")
       While Not parser.EndOfData
          Me.DataGridView1.Rows.Add(parser.ReadFields())
       End
    While
    End
    Using

     

    Thanks 

     

    Friday, May 23, 2008 5:17 PM
  • Just what I was looking for!

    thanks

    Wednesday, June 4, 2008 11:01 PM
  •  

    Nhas,

     

    This:

     

     builder.Append(currentCell.Value)

          builder.Append(",")

     

       next

     

       builder.Append(Environment.NewLine)

     

     

    Could be better changed to this:

     

     

    builder.Append(currentCell.Value)

          builder.AppendLine(",")

     

       next

     

    Thursday, June 5, 2008 1:40 AM
  • Doing this does not give me 1 row per row in the file. Everything is just one long line.

     

     

     

    Thursday, June 5, 2008 9:36 PM
  • I just noticed that I am not getting the Header row. What Am I missing?

     

    For Each currentRow As DataGridViewRow In Me.DGVVERBILLING.Rows

    For Each currentCell As DataGridViewCell In currentRow.Cells

    builder.Append(currentCell.Value)

    builder.Append(",")

    Next

    builder.Append(Environment.NewLine)

    Next

    builder.Remove(builder.Length - 1, 1) 'remove the last comma

    Using writer As New StreamWriter(filelocation)

    writer.Write(builder.ToString())

    writer.Close()

    End Using

     

    Tuesday, September 9, 2008 11:06 PM
  • Wednesday, June 29, 2011 5:47 PM