|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
Note: This is an unedited contribution. If this article is inappropriate,
needs attention or copies someone else's work without reference then please
Report This Article
IntroductionI 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 DescriptionThere 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) ThenfName = OpenFileDialog1.FileName End If Me.TextBox1.Text = fName Dim TextLine As String = "" Dim SplitLine() As StringIf 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 ElseMsgBox( "File Does Not Exist") End If End SubPrivate 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 Elsecellvalue = "" End IfrowLine = rowLine + cellvalue + "," NextobjWriter.WriteLine(rowLine) rowLine = "" NextobjWriter.Close() MsgBox( "Text written to file") Catch e As ExceptionMessageBox.Show( "Error occured while writing to the file." + e.ToString()) FinallyFileClose(1) End Try End SubPrivate Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click SaveGridDataInFile( Me.TextBox1.Text) End SubEnd ClassEnd Source code If you have any queries e-mail me and I will do my best to help. cheers
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||