Displaying Text File in a Table

  • Thread starter Thread starter Amjad Farran
  • Start date Start date
A

Amjad Farran

What is the easiest way to display in a table, a delimited
text data saved in a text file?

I need an idea so that I can research it!

Amjad
 
Hi Amjad,

Just an idea to research

Use a "datatable" and a "datagrid" from which that datatable is the
datasource
(You need for the to create the datatable, the "datarow" 's and the
"datacolumn" 's )

Then you can read your text file using the "streamreader".

While reading record by record you "split" every row using the delimiter

For every row you read you make a datarow which you fill from the splitted
string array.

I think that if you have researched it, your text will be on your screen.

All the words between the "" are words you can look for on/in MSDN, there
are a lot of examples

I hope this was where you where looking for?

Cor
 
Assuming you have a Schema.ini file available for use you can use code like
this:

Public Sub FillTable(ByRef ds As DataSet, ByVal TableName As String, ByVal
Path As String)
Dim strConn, strSQL, strFileTitle, strFileFolder As String
strFileTitle = Path.GetFileName(Path )
strFileFolder = Path.GetDirectoryName(Path )

strConn = "Provider=Microsoft.Jet.OLEDB.4.0;"
strConn &= "Data Source=" & strFileFolder & ";"
strConn &= "Extended Properties=Text"

strSQL = "SELECT * FROM [" & strFileTitle & "]"

Dim dt As DataTable
Dim da As OleDbDataAdapter
Dim cnn As New OleDbConnection(strConn)
Dim cmd As New OleDbCommand(strSQL, cnn)
Try
cmd.CommandType = CommandType.Text
cnn.Open()
da = New OleDbDataAdapter(cmd)
da.Fill(ds, TableName)
Finally
cnn.Close()
End Try
End Sub
 
To display the results:
On a form just add a DataGrid and bind it to your datatable:

DataGrid1.DataSource = dsMain.Tables(strTableName).DefaultView
 
Back
Top