C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Code Snippets » Databinding »

Populate DataGridView using CSV File


Posted Date: 24 Jun 2009    Resource Type: Code Snippets    Category: Databinding
Author: Viji RAJKUMARMember Level: Diamond    
Rating: 1 out of 5Points: 10



The following VB.NET Code is used to populate the DataGridView using CSV File.


I have used a 2 dimensional array to store the content of csv row and column wise


Also 2 One dimensional arrays are used to populate the row and column data respectively.


The logic is to parse the file content into string and store in a two dimensional array.


Then read row wise and column wise and populate the DataGridview.

A new DataGridView Row will be created for each line of data and the cells will be populated with the column data.







Private Sub PopulateDataGridViewFromCSV(ByVal SFileName As String)



'Variables to store the count of rows and columns

Dim nRows As Long

Dim nCols As Long


'Index variables to retrieve the data row wise and col wise

Dim row As Integer

Dim col As Integer



'Declaration of Two dimensional array to store the csv content row wise and column wise


Dim ParseArray(1, 1) As String



'Use Try Catch block to handle exceptions


Try



'Check if file exist


If File.Exists(sFileName) Then



'Open the CSV File into stream



Dim sr As StreamReader = File.OpenText(sFileName)



'Declaration of two single dimensional arrays to store the individual content


Dim RowStringArray() As String


Dim ColStringArray() As String




'Load content of file to RowStringArray array

RowStringArray = sr.ReadToEnd().Split(Environment.NewLine)




'Retrieve the total number of rows

nRows = UBound(RowStringArray)




' Split the content based on "," delimiter

ColStringArray = RowStringArray(0).Split(",")




'Retrieve the total number of columns

nCols = UBound(ColStringArray)



' Redimension the array.

ReDim ParseArray(nRows, nCols)



'Copy the csv content into the 2-dimensional array.
For row = 0 To nRows



ColStringArray = RowStringArray(row).Split(",")



For col = 0 To nCols


ParseArray(row, col) = ColStringArray(col)


Next



Next




'Display the data in ListBox


Dim dgRow As DataGridViewRow



For row = 0 To nRows


For col = 0 To nCols


dgRow = new DataGridViewRow()



dgRow[col] = ParseArray(row,col)



Next



'Add the row

dbGridView.Rows.Add(dgRow)



Next


End If



Catch ex As Exception


MessageBox.Show(ex.Message)


End Try




End Sub








USAGE

PopulateDataGridViewFromCSV ("C:\Test.csv")


will populate the DataGridView With the C:\Test.Csv file.




Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
Populate DataGridView using CSV File  .  

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Storing and Retrieving Images/Files In Sql Server - VB.NET
Previous Resource: Inserting Values in Database
Return to Discussion Resource Index
Post New Resource
Category: Databinding


Post resources and earn money!
 
More Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use