Click here to Skip to main content
Licence CPOL
First Posted 30 Dec 2006
Views 75,561
Bookmarked 50 times

Populating data from a CSV file to a DataGridView and Retriving data from a DataGridView to CSV

By | 30 Dec 2006 | Article
Populating data from a CSV file to a DataGridView and Retriving data from a DataGridView to CSV

 

Introduction

I had problems finding a decent article that showed how to Populate a Datagridview from CSV file and how to store information from DataGridView to a CSV file.

This article is compilation of two sources from the internet.  First one is http://www.thescripts.com/forum/thread554150.html and the second one is http://www.homeandlearn.co.uk/NET/vbNet.html

Description

There is one Form having default name Form1 

There is one DataGridView named DataGridView1

Two buttons named Button1 and Button2

One Textbox named Textbox1

The csv file contains information as follows (between the inverted commas):

"Dharmit,Male,28,First

Lomesha,Female,26,Second

Jaymit,Male,24,Third

Ambrish,Male,54,First

Chanda,Female,50,Second"

Remove inverted commas if you are copying directly from here

 

The sourcecode behind the form is

SourceCode

Public Class Form1

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

Dim fName As String = ""

OpenFileDialog1.InitialDirectory = "c:\temp\"

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

 

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)

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()

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

End Sub

 

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

SaveGridDataInFile(Me.TextBox1.Text)

End Sub

End Class

End Source code

If you have any queries e-mail me and I will do my best to help.

cheers

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Dharmit

Engineer

Australia Australia

Member

I started with C and then my Professor introduced C++ and I was thrilled. Then when I was working for an Engineering firm, I was introduced to VB6 by one of my friend (and till day I am thankful to him). My love for 3D graphics made me learn 3D studio max but left it and started learning OpenGL. Frustrated with it when I couldn't work with OpenGL in VB6 and VB.net properly, I switched to DirectX. I am learning it for sometimes and will be looking forward to it. Currently I started working on developing Pipe StressAnalysis software.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralData from txt file [modified] Pinmembersolfinker1:30 22 Apr '11  
GeneralImport csv to database PinmemberAnthony Fonseca7:43 21 Mar '11  
GeneralMy vote of 5 PinmemberAnthony Fonseca16:31 16 Mar '11  
Generaljust what the doctor recommended Pinmembergiovetti6:07 25 Feb '11  
GeneralThis is really really great Pinmembersolfinker9:10 29 Jan '11  
GeneralRe: This is really really great PinmemberDharmit16:46 29 Jan '11  
GeneralRe: This is really really great Pinmembersolfinker22:01 29 Jan '11  
GeneralRe: This is really really great Pinmembersolfinker0:00 30 Jan '11  
QuestionError Details. Pinmembersamvaidy8:30 10 Mar '10  
AnswerRe: Error Details. PinmemberDharmit12:24 10 Mar '10  
GeneralThank you! PinmemberElizabeth Tweedale1:05 7 Oct '09  
QuestionDatagridview Validation PinmemberMatikule4:48 7 Nov '08  
GeneralReading a CSV into a DataGridView PinmemberFilemon Tanchoco19:25 14 Jun '08  
GeneralRe: Reading a CSV into a DataGridView PinmemberDharmit1:07 15 Jun '08  
GeneralGreat Code - Exactly what I was looking for. Pinmemberbaillie1123:27 14 May '08  
GeneralRe: Great Code - Exactly what I was looking for. PinmemberDharmit1:08 15 Jun '08  
QuestionThis Doesn't Work Pinmemberjcviera7:18 19 Jan '07  
AnswerRe: This Doesn't Work Pinmembervishal_chauhan7:43 21 Jun '07  
GeneralGreat PinmemberMarwa ElBadry21:06 2 Jan '07  
GeneralRe: Great PinmemberDharmit0:33 3 Jan '07  
GeneralRe: Great PinmemberKwazai3:10 4 Jan '07  
GeneralRe: Great PinmemberLuca Crisi, MCP19:23 21 Dec '08  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120528.1 | Last Updated 30 Dec 2006
Article Copyright 2006 by Dharmit
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid