views:

565

answers:

1

I have a DataGridView set up with the following columns:

Teacher, Subject, Date, Period.

After a lot of Googleing I can see there are a few ways to add data to the grid programmatically, with each one differing to another quite extensively.

I wanted your opinion on how I should go about this, considering I am going to be adding data from a text file line-by-line (using ":" as a delimiter) and I want each line to have its own row, so it's going to be in a loop.

Thanks.

+1  A: 

Since the data in the text file is delimited then one way to do this is to use the Split function to create an array of cell strings and then just add these directly to the grid.

Dim CellData() As String
Dim LineText As String = ""

' open the data file
Dim objReader As New System.IO.StreamReader("c:\temp\file.dat")

Do While objReader.Peek() <> -1
    LineText = objReader.ReadLine()
    ' split the line of text into cells
    CellData = Split(LineText, ":")
    Me.DataGridView1.Rows.Add(CellData)
Loop
Blake7