Code and Snippets for vb.net 2005
RSS icon Email icon Home icon
  • Loading (.csv) file to listview

    Posted on July 29th, 2009 shyguy 2 comments

    Well, somebody is asking me for a code that will load up a (.csv) format to listview. In response to what he wanted, I decided to put it in here based on how I load a .csv file to listview. This article will be in relation to the previous post that I made. I hope this will help. Enjoy!


    Private Sub LoadData()
    Dim fileLookUp As New OpenFileDialog
    fileLookUp.Title = "Get File"
    fileLookUp.Filter = "Files (*.csv)|*.csv|All Files (*.*)|*.*"
    If fileLookUp.ShowDialog() = Windows.Forms.DialogResult.OK Then
    Me.txtFile.Text = fileLookUp.FileName
    mFileName = System.IO.Path.GetFileName(Me.txtFile.Text)
    mFolder = System.IO.Path.GetDirectoryName(Me.txtFile.Text)
    End If

    Dim ConnectionString, CommandText As String
    Dim conn As OleDb.OleDbConnection
    Dim Command As OleDbCommand
    Dim myString As String = txtFile.Text
    Dim myIndex As Integer = myString.LastIndexOf("\") + 1
    myString = myString.Substring(myIndex, myString.Length - myIndex)

    Try

    ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & mFolder & "\;Extended Properties='text;HDR=Yes'"

    CommandText = "select * from " & myString & ""

    conn = New System.Data.OleDb.OleDbConnection(ConnectionString)
    Command = New System.Data.OleDb.OleDbCommand(CommandText, conn)

    conn.Open()

    Dim t As New DataTable
    ' initializing its column to complement on how many fields I want to return in my query command
    t.Columns.Add("v7#3")
    t.Columns.Add("F2")
    t.Columns.Add("F3")

    ' a method to access read-only the result set.
    Dim reader As OleDbDataReader = Command.ExecuteReader()
    While reader.Read()
    ' create new row
    Dim r As DataRow = t.NewRow()
    r(0) = reader("v7#3")
    r(1) = reader("F2")
    r(2) = reader("F3")

    ' add a row to a datatable
    t.Rows.Add(r)
    End While

    ' close reader
    reader.Close()
    ' close the connection

    conn.Close()

    For i As Integer = 0 To t.Rows.Count - 1
    Dim li As ListViewItem = ListView1.Items.Add(t.Rows(i)("v7#3").ToString())
    li.SubItems.Add(t.Rows(i)("F2").ToString())
    li.SubItems.Add(t.Rows(i)("F3").ToString())

    Next

    Catch ex As Exception
    MsgBox("Invalid Format! Please try another.")
    End Try
    End Sub

     

    2 responses to “Loading (.csv) file to listview” RSS icon

    • I used this one to load .cvs :

      Private Sub CargarInfoToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CargarInfoToolStripMenuItem.Click

      ‘FUNCION PARA EL CARGAR DATOS O EL LOAD !!
      Dim TotalNombres, Cumpleanieros, HoyDia, HoyMes, HOY As String
      Dim elimextra, y As Integer
      Dim sPathCsv As String
      Dim sDelimitador As String

      Try ‘ ABRE EL DIALOG BOX

      Dim filename As String
      filename = “nada”

      ‘They want to do a SaveAs, so find out what they want to name the file

      Dim openFileDialog As OpenFileDialog = New OpenFileDialog()

      openFileDialog.Title = “Open File”
      openFileDialog.Filter = “Files (*.csv)|*.csv|All Files (*.*)|*.*”

      openFileDialog.DefaultExt = “csv”

      openFileDialog.AddExtension = True

      If openFileDialog.ShowDialog(Me) = Windows.Forms.DialogResult.OK Then

      filename = openFileDialog.FileName

      ElseIf Windows.Forms.DialogResult.Cancel Then

      Exit Sub

      End If

      ListView1.Clear()
      ListView1.Columns.Add(”Nombres”).Width = 75
      ListView1.Columns.Add(”Apellidos”).Width = 75
      ListView1.Columns.Add(”Dia”).Width = 50
      ListView1.Columns.Add(”Mes”).Width = 50
      ListView1.Columns.Add(”Año”).Width = 50
      ListView1.Columns.Add(”Hora”).Width = 50
      ListView1.Columns.Add(”Minuto”).Width = 50
      ListView1.Columns.Add(”Email”).Width = 75
      ListView1.Columns.Add(”Email”).Width = 75
      ListView1.Columns.Add(”Web”).Width = 75
      ListView1.Columns.Add(”Telefono”).Width = 75
      ListView1.Columns.Add(”Celular”).Width = 75
      ListView1.Columns.Add(”Foto”).Width = 75
      ListView1.Columns.Add(”Info”).Width = 75
      ListView1.Columns.Add(”Fecha De Captura”).Width = 75

      With ListView1

      sPathCsv = filename
      sDelimitador = “,”
      ‘ recorre el vector y añade las cabeceras

      ‘ Abre el archivo para leer cada línea
      Dim sr As New IO.StreamReader(sPathCsv)
      Dim aDatos() As String ‘ vector para el cvs

      ‘ recorrer todas las líneas hasta el final del archivo
      Do While (sr.Peek >= 0)
      ‘ leer la línea y separar los datos con split
      aDatos = sr.ReadLine.Split(sDelimitador)

      ‘ listview: Añadir los items y SubItems
      ””””””””””””””””””””””””
      Dim Item As New ListViewItem(aDatos(0).ToString)
      For i As Integer = 1 To UBound(aDatos)
      With Item
      .SubItems.Add(aDatos(i).ToString)
      End With
      Next
      .Items.Add(Item)
      Loop
      sr.Close() ‘ cierra el streamReader
      End With

      ListView1.Items(0).Remove() ‘Elimina Primera Linea que es TITULO

      elimextra = ListView1.Items.Count - 1 ‘Linea Extra se añadió

      ListView1.Items(elimextra).Remove() ‘Se Elimina esta linea extra

      ‘ error

      Catch ex As Exception
      MsgBox(ex.Message.ToString, MsgBoxStyle.Critical)
      End Try

      ‘/ La funcion que pinta de colores las líneas
      ‘For i = 1 To ListView1.Items.Count Step 2
      ‘ListView1.Items(i).BackColor = Drawing.Color.LightGray
      ‘Next i

      ‘CONTADOR EN MAIN
      TextBoxPersonasCapturadas.Text = ListView1.Items.Count

      ‘AVISO DE CUMPLEAÑOS DE HOY
      HOY = DateTime.Now
      HoyDia = Mid(HOY, 1, 2)
      HoyMes = Mid(HOY, 4, 2)
      TotalNombres = ListView1.Items.Count

      For y = 1 To TotalNombres
      If HoyDia = ListView1.Items(y - 1).SubItems(2).Text And HoyMes = ListView1.Items(y - 1).SubItems(3).Text Then
      Cumpleanieros = “SI”
      End If
      Next

      If Cumpleanieros = “SI” Then
      MsgBox(”Cumpleaños Presentes Para Hoy “, MsgBoxStyle.Information, “Aviso De Cumpleaños”)
      End If
      End Sub

    • the code I place here its programmed in Visual Basic Express 2008 :P , works for me and has a few spanish languague .. jejeje


    Leave a reply