Click here to Skip to main content
14,920,432 members
Articles / Programming Languages / Visual Basic
Posted 30 Dec 2006


53 bookmarked

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

Rate me:
Please Sign up or sign in to vote.
3.35/5 (20 votes)
30 Dec 2006CPOL2 min read
Populating data from a CSV file to a DataGridView and Retriving data from a DataGridView to CSV



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 and the second one is


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






Remove inverted commas if you are copying directly from here


The sourcecode behind the form is


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, ",")




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 = ""


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


cellvalue = ""

End If

rowLine = rowLine + cellvalue + ","



rowLine = ""



MsgBox("Text written to file")

Catch e As Exception

MessageBox.Show("Error occured while writing to the file." + e.ToString())



End Try

End Sub


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


End Sub

End Class

End Source code

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



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


About the Author

Australia Australia
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 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.

Comments and Discussions

QuestionErrror in import data from csv to datagridview Pin
Rahu3212-Apr-17 23:28
MemberRahu3212-Apr-17 23:28 
QuestionHow to add csv data to DataGridView using VB 2010? Pin
Member 1036332728-Oct-13 17:08
MemberMember 1036332728-Oct-13 17:08 
GeneralMy vote of 5 Pin
andyjohnwhite10-Sep-12 8:01
Memberandyjohnwhite10-Sep-12 8:01 
GeneralData from txt file [modified] Pin
solfinker22-Apr-11 1:30
Membersolfinker22-Apr-11 1:30 
GeneralImport csv to database Pin
Anthony Fonseca21-Mar-11 7:43
MemberAnthony Fonseca21-Mar-11 7:43 
GeneralMy vote of 5 Pin
Anthony Fonseca16-Mar-11 16:31
MemberAnthony Fonseca16-Mar-11 16:31 
Generaljust what the doctor recommended Pin
giovetti25-Feb-11 6:07
Membergiovetti25-Feb-11 6:07 
GeneralThis is really really great Pin
solfinker29-Jan-11 9:10
Membersolfinker29-Jan-11 9:10 
GeneralRe: This is really really great Pin
Dharmit29-Jan-11 16:46
MemberDharmit29-Jan-11 16:46 
GeneralRe: This is really really great Pin
solfinker29-Jan-11 22:01
Membersolfinker29-Jan-11 22:01 
GeneralRe: This is really really great Pin
solfinker30-Jan-11 0:00
Membersolfinker30-Jan-11 0:00 
QuestionError Details. Pin
samvaidy10-Mar-10 8:30
Membersamvaidy10-Mar-10 8:30 
AnswerRe: Error Details. Pin
Dharmit10-Mar-10 12:24
MemberDharmit10-Mar-10 12:24 
GeneralThank you! Pin
Elizabeth Tweedale7-Oct-09 1:05
MemberElizabeth Tweedale7-Oct-09 1:05 
QuestionDatagridview Validation Pin
Matikule7-Nov-08 4:48
MemberMatikule7-Nov-08 4:48 
GeneralReading a CSV into a DataGridView Pin
Filemon Tanchoco14-Jun-08 19:25
MemberFilemon Tanchoco14-Jun-08 19:25 
GeneralRe: Reading a CSV into a DataGridView Pin
Dharmit15-Jun-08 1:07
MemberDharmit15-Jun-08 1:07 
GeneralGreat Code - Exactly what I was looking for. Pin
baillie1114-May-08 23:27
Memberbaillie1114-May-08 23:27 
GeneralRe: Great Code - Exactly what I was looking for. Pin
Dharmit15-Jun-08 1:08
MemberDharmit15-Jun-08 1:08 
QuestionThis Doesn't Work Pin
jcviera19-Jan-07 7:18
Memberjcviera19-Jan-07 7:18 
AnswerRe: This Doesn't Work Pin
vishal_chauhan21-Jun-07 7:43
Membervishal_chauhan21-Jun-07 7:43 
GeneralGreat Pin
Marwa ElBadry2-Jan-07 21:06
MemberMarwa ElBadry2-Jan-07 21:06 
GeneralRe: Great Pin
Dharmit3-Jan-07 0:33
MemberDharmit3-Jan-07 0:33 
GeneralRe: Great Pin
Kwazai4-Jan-07 3:10
MemberKwazai4-Jan-07 3:10 
GeneralRe: Great Pin
Luca Crisi, MCP21-Dec-08 19:23
MemberLuca Crisi, MCP21-Dec-08 19:23 

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

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