VB.Net - CSV To DataGridView

This code snippet shows how to populate a DataGridView control from a CSV file using the StreamReader class.
  1. // Contact.csv
  2.  
  3. John,Smith,john.smith@xyzmail.com
  4. Suzan,Carter,suzan.carter@xyzmail.com
  5. Tom,Baker,tom.baker@xyzmail.com
  6.  
  7.  
  8. Imports System.IO
  9.  
  10. Public Class Form1
  11.  
  12.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  13.  
  14.         Dim sReader As New StreamReader("Contact.csv")
  15.         Dim Record() As String
  16.  
  17.         DataGridView1.Columns.Add("FirstName", "First Name")
  18.         DataGridView1.Columns.Add("LastName", "Last Name")
  19.         DataGridView1.Columns.Add("Email", "Email")
  20.  
  21.         While sReader.Peek() <> -1
  22.             Record = sReader.ReadLine().Split(",")
  23.             DataGridView1.Rows.Add(Record)
  24.  
  25.         End While
  26.  
  27.     End Sub
  28. End Class

No comments:

Post a Comment