Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

CSV to listcontrol

Status
Not open for further replies.

russgreen

Programmer
Dec 7, 2002
86
GB
I'm trying to build a method to populate a listcontrol from a csv file. My immediate requirement is to read a two column csv file and use the fist column a listcontrol.valuemember and the second column of the csv file to be listcontrol.displaymember

My code so far,

Code:
    Dim sAppPath As String = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly.Location)

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        CSVToListControl1(Me.ComboBox1, sAppPath + "\sample.csv")
    End Sub

    Private Sub CSVToListControl1(ByVal ListControl As ListControl, ByVal sCSVFile As String)
        Dim csv_data(0, 0) As String
        Dim r As Integer 'rows
        Dim c As Integer 'cols

        'load the two column CSV file to an array
        Dim stream_reader As New StreamReader(sCSVFile)
        Dim file_contents As String = stream_reader.ReadToEnd()
        stream_reader.Close()

        'break the file into lines.
        Dim lines() As String = file_contents.Split(CChar(vbCrLf))

        'break the line into columns
        Dim one_line() As String = Split(lines(0), ",")

        ' Dimension the array.
        Dim num_rows As Integer
        Dim num_cols As Integer
        num_rows = UBound(lines)
        num_cols = UBound(one_line)

        ReDim csv_data(num_rows, num_cols)

        'copy the data into the array.
        For r = 0 To num_rows - 1
            one_line = Split(lines(r), ",")
            For c = 0 To num_cols
                csv_data(r, c) = one_line(c)
            Next c
        Next r

        ' Prove we have the data loaded.
        For r = 0 To num_rows - 1
            For c = 0 To num_cols
                Debug.Write(csv_data(r, c) & "|")
            Next c
        Next r
    End Sub

so how do a get the data from the array either straight into the listcontrol or into a dataset?

TIA

Russ
 
Visit the following link:


I have done something similar in this sample. The DataGrid is bounded to a DataTable which is filled from text file. It also creates column names in the DataTable if your text file contains column names in the first row.

Tweak the code from this sample to fill the DataTable object and bind your listcontrol to the DataTable.


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top