Click here to Skip to main content
12,691,097 members (32,689 online)
Click here to Skip to main content
Add your own
alternative version

Stats

145.2K views
53 bookmarked
Posted

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

, 30 Dec 2006 CPOL
Rate this:
Please Sign up or sign in to vote.
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

<FONT color=#0000ff size=2>Public <FONT color=#0000ff size=2>Class Form1

<FONT color=#0000ff size=2>Private <FONT color=#0000ff size=2>Sub Button1_Click(<FONT color=#0000ff size=2>ByVal sender <FONT color=#0000ff size=2>As System.Object, <FONT color=#0000ff size=2>ByVal e <FONT color=#0000ff size=2>As System.EventArgs) <FONT color=#0000ff size=2>Handles Button1.Click

<FONT color=#0000ff size=2>Dim fName <FONT color=#0000ff size=2>As <FONT color=#0000ff size=2>String = <FONT color=#800000 size=2>""

OpenFileDialog1.InitialDirectory = <FONT color=#800000 size=2>"c:\temp\"

OpenFileDialog1.Filter = <FONT color=#800000 size=2>"CSV files (*.csv)|*.CSV"

OpenFileDialog1.FilterIndex = 2

OpenFileDialog1.RestoreDirectory = <FONT color=#0000ff size=2>True

<FONT color=#0000ff size=2>If (OpenFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK) <FONT color=#0000ff size=2>Then

fName = OpenFileDialog1.FileName

<FONT color=#0000ff size=2>End <FONT color=#0000ff size=2>If

<FONT color=#0000ff size=2>Me.TextBox1.Text = fName

<FONT color=#0000ff size=2>Dim TextLine <FONT color=#0000ff size=2>As <FONT color=#0000ff size=2>String = <FONT color=#800000 size=2>""

<FONT color=#0000ff size=2>Dim SplitLine() <FONT color=#0000ff size=2>As <FONT color=#0000ff size=2>String

 

<FONT color=#0000ff size=2>If System.IO.File.Exists(fName) = <FONT color=#0000ff size=2>True <FONT color=#0000ff size=2>Then

<FONT color=#0000ff size=2>Dim objReader <FONT color=#0000ff size=2>As <FONT color=#0000ff size=2>New System.IO.StreamReader(fName)

<FONT color=#0000ff size=2>Do <FONT color=#0000ff size=2>While objReader.Peek() <> -1

TextLine = objReader.ReadLine()

SplitLine = Split(TextLine, <FONT color=#800000 size=2>",")

<FONT color=#0000ff size=2>Me.DataGridView1.Rows.Add(SplitLine)

<FONT color=#0000ff size=2>Loop

<FONT color=#0000ff size=2>Else

MsgBox(<FONT color=#800000 size=2>"File Does Not Exist")

<FONT color=#0000ff size=2>End <FONT color=#0000ff size=2>If

<FONT color=#0000ff size=2>End <FONT color=#0000ff size=2>Sub

<FONT color=#0000ff size=2> 

<FONT color=#0000ff size=2>Private <FONT color=#0000ff size=2>Sub SaveGridDataInFile(<FONT color=#0000ff size=2>ByRef fName <FONT color=#0000ff size=2>As <FONT color=#0000ff size=2>String)

<FONT color=#0000ff size=2>Dim I <FONT color=#0000ff size=2>As <FONT color=#0000ff size=2>Integer = 0

<FONT color=#0000ff size=2>Dim j <FONT color=#0000ff size=2>As <FONT color=#0000ff size=2>Integer = 0

<FONT color=#0000ff size=2>Dim cellvalue$

<FONT color=#0000ff size=2>Dim rowLine <FONT color=#0000ff size=2>As <FONT color=#0000ff size=2>String = <FONT color=#800000 size=2>""

<FONT color=#0000ff size=2>Try

<FONT color=#0000ff size=2>Dim objWriter <FONT color=#0000ff size=2>As <FONT color=#0000ff size=2>New System.IO.StreamWriter(fName, <FONT color=#0000ff size=2>True)

<FONT color=#0000ff size=2>For j = 0 <FONT color=#0000ff size=2>To (DataGridView1.Rows.Count - 2)

<FONT color=#0000ff size=2>For I = 0 <FONT color=#0000ff size=2>To (DataGridView1.Columns.Count - 1)

<FONT color=#0000ff size=2>If <FONT color=#0000ff size=2>Not <FONT color=#0000ff size=2>TypeOf DataGridView1.CurrentRow.Cells.Item(I).Value <FONT color=#0000ff size=2>Is DBNull <FONT color=#0000ff size=2>Then

cellvalue = DataGridView1.Item(I, j).Value

<FONT color=#0000ff size=2>Else

cellvalue = <FONT color=#800000 size=2>""

<FONT color=#0000ff size=2>End <FONT color=#0000ff size=2>If

rowLine = rowLine + cellvalue + <FONT color=#800000 size=2>","

<FONT color=#0000ff size=2>Next

objWriter.WriteLine(rowLine)

rowLine = <FONT color=#800000 size=2>""

<FONT color=#0000ff size=2>Next

objWriter.Close()

MsgBox(<FONT color=#800000 size=2>"Text written to file")

<FONT color=#0000ff size=2>Catch e <FONT color=#0000ff size=2>As Exception

MessageBox.Show(<FONT color=#800000 size=2>"Error occured while writing to the file." + e.ToString())

<FONT color=#0000ff size=2>Finally

FileClose(1)

<FONT color=#0000ff size=2>End <FONT color=#0000ff size=2>Try

<FONT color=#0000ff size=2>End <FONT color=#0000ff size=2>Sub

<FONT color=#0000ff size=2> 

<FONT color=#0000ff size=2>Private <FONT color=#0000ff size=2>Sub Button2_Click(<FONT color=#0000ff size=2>ByVal sender <FONT color=#0000ff size=2>As System.Object, <FONT color=#0000ff size=2>ByVal e <FONT color=#0000ff size=2>As System.EventArgs) <FONT color=#0000ff size=2>Handles Button2.Click

SaveGridDataInFile(<FONT color=#0000ff size=2>Me.TextBox1.Text)

<FONT color=#0000ff size=2>End <FONT color=#0000ff size=2>Sub

End <FONT color=#0000ff size=2>Class

End Source code

<FONT color=#0000ff size=2>If you have any queries e-mail me and I will do my best to help.

<FONT color=#0000ff size=2>cheers

License

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

Share

About the Author

Dharmit
Engineer
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 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.

You may also be interested in...

Pro

Comments and Discussions

 
QuestionHow to add csv data to DataGridView using VB 2010? Pin
Member 1036332728-Oct-13 18:08
memberMember 1036332728-Oct-13 18:08 
GeneralMy vote of 5 Pin
andyjohnwhite10-Sep-12 9:01
memberandyjohnwhite10-Sep-12 9:01 
GeneralData from txt file [modified] Pin
solfinker22-Apr-11 2:30
membersolfinker22-Apr-11 2:30 
GeneralImport csv to database Pin
Anthony Fonseca21-Mar-11 8:43
memberAnthony Fonseca21-Mar-11 8:43 
GeneralMy vote of 5 Pin
Anthony Fonseca16-Mar-11 17:31
memberAnthony Fonseca16-Mar-11 17:31 
Generaljust what the doctor recommended Pin
giovetti25-Feb-11 7:07
membergiovetti25-Feb-11 7:07 
GeneralThis is really really great Pin
solfinker29-Jan-11 10:10
membersolfinker29-Jan-11 10:10 
GeneralRe: This is really really great Pin
Dharmit29-Jan-11 17:46
memberDharmit29-Jan-11 17:46 
GeneralRe: This is really really great Pin
solfinker29-Jan-11 23:01
membersolfinker29-Jan-11 23:01 
GeneralRe: This is really really great Pin
solfinker30-Jan-11 1:00
membersolfinker30-Jan-11 1:00 
QuestionError Details. Pin
samvaidy10-Mar-10 9:30
membersamvaidy10-Mar-10 9:30 
AnswerRe: Error Details. Pin
Dharmit10-Mar-10 13:24
memberDharmit10-Mar-10 13:24 
GeneralThank you! Pin
Elizabeth Tweedale7-Oct-09 2:05
memberElizabeth Tweedale7-Oct-09 2:05 
QuestionDatagridview Validation Pin
Matikule7-Nov-08 5:48
memberMatikule7-Nov-08 5:48 
GeneralReading a CSV into a DataGridView Pin
Filemon Tanchoco14-Jun-08 20:25
memberFilemon Tanchoco14-Jun-08 20:25 
GeneralRe: Reading a CSV into a DataGridView Pin
Dharmit15-Jun-08 2:07
memberDharmit15-Jun-08 2:07 
GeneralGreat Code - Exactly what I was looking for. Pin
baillie1115-May-08 0:27
memberbaillie1115-May-08 0:27 
GeneralRe: Great Code - Exactly what I was looking for. Pin
Dharmit15-Jun-08 2:08
memberDharmit15-Jun-08 2:08 
QuestionThis Doesn't Work Pin
jcviera19-Jan-07 8:18
memberjcviera19-Jan-07 8:18 
AnswerRe: This Doesn't Work Pin
vishal_chauhan21-Jun-07 8:43
membervishal_chauhan21-Jun-07 8:43 
GeneralGreat Pin
Marwa ElBadry2-Jan-07 22:06
memberMarwa ElBadry2-Jan-07 22:06 
GeneralRe: Great Pin
Dharmit3-Jan-07 1:33
memberDharmit3-Jan-07 1:33 
GeneralRe: Great Pin
Kwazai4-Jan-07 4:10
memberKwazai4-Jan-07 4:10 
GeneralRe: Great Pin
Luca Crisi, MCP21-Dec-08 20:23
memberLuca Crisi, MCP21-Dec-08 20: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.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170117.1 | Last Updated 30 Dec 2006
Article Copyright 2006 by Dharmit
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid