VB.net import some columns (not all) from csv in datagridview

  • Thread starter Thread starter Balacche4
  • Start date Start date
B

Balacche4

Guest
Hi,

I'm sorry, I'm not very expert to VB.NET.

I'm trying to import only any columns from CSV to DataGridView using this code:

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)
Me.DataGridView1.ColumnCount = 10
Me.DataGridView1.ColumnHeadersVisible = True
Me.DataGridView1.Columns(0).Name = "Username"
Me.DataGridView1.Columns(1).Name = "IP"
Me.DataGridView1.Columns(2).Name = "Location"

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



But this code, import all columns from CSV and the first row is the column header as below:

1564336.png


How can modify my code, to insert only columns 1, 2 and 5 for example, and the first row use as columns header?

Thanks for the support.

Marco

Continue reading...
 
Back
Top