K. Arth
K. Arth

Reputation: 17

How to import .csv file to my datagridview in vb.net?

My used code is:

Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
    Dim fName As String = ""
    OpenFileDialog1.InitialDirectory = "c:\desktop"
    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
    txtpathfile.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.DataGridView2.Rows.Add(SplitLine)
        Loop
    Else
        MsgBox("File Does Not Exist")
    End If
End Sub

My output looks wrongly encoded: enter image description here

What's wrong with my code? Please help me. Thank you for consideration.

Upvotes: 2

Views: 19041

Answers (3)

Alfa Rojo
Alfa Rojo

Reputation: 194

For anyone that has been looking for other solution, this works and let me add, delete and update the same datagrid without any problem

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Using ofd As OpenFileDialog = New OpenFileDialog()
            If ofd.ShowDialog() = DialogResult.OK Then
                file_path.Text = ofd.FileName
                Dim lines As List(Of String) = File.ReadAllLines(ofd.FileName).ToList()
                Dim list As List(Of User) = New List(Of User)
                fill_Columns(lines)
                For i As Integer = 1 To lines.Count - 1
                    Dim data As String() = lines(i).Split(",")
                    addElements(
                    data(0),
                    data(1),
                    data(2),
                    data(3),
                    data(4)
                ) 'All this elements has to match with the -fill_Columns- elements
                Next
            End If
        End Using
    End Sub

Use it on the button where you want to click it... Also add this

    Private Sub fill_Columns(lines As List(Of String))
        Dim columns() As String = lines(0).Split(",")
        For Each item In columns
            Dim col As New DataGridViewTextBoxColumn
            col.HeaderText = item
            col.Name = item
            col.DataPropertyName = item
            DataGridView1.Columns.Add(col)
        Next
    End Sub

    Private Sub addElements(Code As String, Name As String, Birth As DateTime, Email As String, Address As String)
            DataGridView1.Rows.Add(
                Code,
                Name,
                Birth.ToString,
                Email,
                Address
            ) 'You add or delete as you need
    End Sub

Upvotes: 1

Trexer
Trexer

Reputation: 31

I arrived here from google. Just in case someone sarching for a quick code to copy:

    Private Sub ReadCSV()
        Dim fName As String = "C:\myfile.csv"
        Dim TextLine As String = ""
        Dim SplitLine() As String

        If System.IO.File.Exists(fName) = True Then
            Using objReader As New System.IO.StreamReader(fName, Encoding.ASCII)
                Do While objReader.Peek() <> -1
                    TextLine = objReader.ReadLine()
                    SplitLine = Split(TextLine, ";")
                    Me.DataGridView1.Rows.Add(SplitLine)
                Loop
            End Using
        Else
            MsgBox("File Does Not Exist")
        End If
    End Sub

Upvotes: 3

K. Arth
K. Arth

Reputation: 17

This is really work!

Dim fName As String = ""
   OpenFileDialog1.InitialDirectory = "c:\desktop"
       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
    txtpathfile.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(txtpathfile.Text, Encoding.ASCII)
        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

Upvotes: -2

Related Questions