DataBinding using a sequential file

  • Thread starter Thread starter Tom McLaughlin
  • Start date Start date
T

Tom McLaughlin

I am new to vb.net and I am running into problems understanding
DataBinding and its concept. The only examples I find are always
talk about Web design, I only want to use this data on my
standalone computer. So my question is:

I have a sequential file that contains data and the data is structured.
is there a way to load this data into a DataGrid control?

If the answer is yes, then how do I accomplish this?

I am using vb.net the STANDARD version 2003.

Thanks

Tom
 
Hi Tom,

A complete simple sample for your question that I have made a week ago.

The textfile is a csv file which has the comma delimiter in your culture
setting.

I hope this helps?

Cor
\\\drag a datagrid on your form and make a simple textfile
Private Sub Form1_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
Dim file As String = "Test2.txt"
Dim path As String = "C:\Test1\"
Dim ds As New DataSet
Try
Dim f As System.IO.File
If f.Exists(path & file) Then
Dim ConStr As String = _
"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & _
path & ";Extended Properties=""Text;HDR=No;FMT=Delimited\"""
Dim conn As New OleDb.OleDbConnection(ConStr)
Dim da As New OleDb.OleDbDataAdapter("Select * from " & _
file, conn)
da.Fill(ds, "TextFile")
End If
Catch ex As Exception
MessageBox.Show(ex.ToString)
End Try
DataGrid1.DataSource = ds.Tables(0)
End Sub
//
 
Well.. Yes... you can... but coming from a flat file your going to struggle
a little.

In order for databinding to work within windows forms (particularly a grid)
a bindable class must implement ITypedList and IBindingList (for datagrid).
The minimum to do any binding is IList.

So.. the best approach for you is to build a class and derive IList for
starters just to get a feel of how it works (I think IList only has like 3
or 4 members you have to implement) and you can then bind that class. Now
this class will read in your sequential file and expose it to .NET in a
manner they both agree on (the IList Interface), and thats about it.

So.. your class reads in the file and implements IList... so you may have
something.

Public MyReadingClass Implements IList
...implementation and such
End Class

Public class myForm
inherits System.Windows.Forms.Form

public sub form_load(sender as object, e as system.eventargs) handles
mybase.load

dim myReader as new MyReadingClass

myDataGrid.DataSource = myReader
End Sub
End Class

This will give you a basic "list" (no column names, not multiple columns as
far as I know, just a list) but its a starting point for learning interfaces
and understanding IBindingList, and ITypedList.

Thats when it gets really fun (I'm serious, writing custom data binding
classes is fun!)

HTH,
CJ
 
Back
Top