Datatables

Joined
Oct 28, 2006
Messages
19
Programming Experience
Beginner
Having looked at multidimensional arrays with some degree of confusion I was told a datatable might work better for me. I need to read strings of data from a text file which has 9 elements which are comma delimited. How do i use a datatable with this and then how do i display this. Any help is greatly appreciated. This is rather time critical so if anyone could describe the process in some detail it would be greatly appreciated
 
Hi programmingstudent,

Having read your previous post, I'll tell you how I'd go about it from an OO perpective, other may disagree. Create a new class to store a single row from your file and give it properties related to the values from the file for example if the row represented a person, a class as follows:-

VB.NET:
Public Class Person

Private _firstname as String
Private _lastname as String
Private _address as String
Private _age as integer
Private _isafriend as boolean

 Public Property Firstname() As String
        Get
            return _firstname
        End Get
        Set(ByVal Value As String)
            _firstname = Value
        End Set
End Property

 Public Property Lastname() As String
        Get
            return _lastname
        End Get
        Set(ByVal Value As String)
            _lastname = Value
        End Set
End Property

etc........

End Class
Then for every row you read create a new Person (or whatever your new class is) and populate it's properties ie..

VB.NET:
'Global variable
dim MyPeople as New Arraylist

'Inside reading sub
dim tmpPerson as Person

'inside row reading loop
tmpPerson = New Person
tmpPerson.Firstname = myarrayreadfromline(0)
tmpPerson.Lastname = myarrayreadfromline(1)
etc...

MyPeople.Add(tmpPerson)
Once the arraylist is populated you can iterate through it thus:-

VB.NET:
For each tmpPerson as Person in MyPeople
MessageBox.Show(tmpPerson.firstname & " " & tmpPerson.lastname)
Next
You could also use a datatable by setting up it's columns to be the value you need to store and adding / removing rows.

If you do want to use a datatable there's a good tutorial here:-
http://www.startvbdotnet.com/ado/datatable.aspx

I hope this helps,

Regards,

Andy
 
Last edited:
Thanks for that, i followed what was going on until the second section where tmpperson as new person comes up with declaration expected. Did i put something in the wrong place or do i need to add something else. Sorry if i sound like an idiot im very new to this
 
Hey, I've edited the post (made a slight mistake) but you should be declaring it outside your loop ie.. dim tmpPerson as Person (not New person) then reuse that declaration inside the loop ie tmpPerson = New Person.
You don't sound stupid at all.. once you've got your head round the OO stuff the world is your oyster.
I personally wouldn't use a datatable in this instance as using your own objects provides much more power.. ie you could add a public subs to the person class to provide more functionality.

Edit: Just to pre empt some common pitfalls...

The arraylist is a lovely bit of kit but it's not type safe.. ie you can store anything in there. So when you want to fetch a person from it you should cast the type something like..

dim tmpPerson as Person

tmpPerson = CType(MyPeople(2), Person)

Regards,

Andy
 
Ok ive almost got it to work only problem is it only does it for the first record can you point out the error in my code

VB.NET:
[SIZE=2][COLOR=#0000ff]
Private[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] test5_Click([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Object, [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.EventArgs) [/SIZE][SIZE=2][COLOR=#0000ff]Handles[/COLOR][/SIZE][SIZE=2] test5.Click
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] sr [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] IO.StreamReader
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] tmpPerson [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] Person
[/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] IO.File.Exists("NEWRECORD.TXT") [/SIZE][SIZE=2][COLOR=#0000ff]Then
[/COLOR][/SIZE][SIZE=2]sr = IO.File.OpenText("NEWRECORD.TXT")
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] strA [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]String
[/COLOR][/SIZE][SIZE=2]strA = sr.ReadLine
[/SIZE][SIZE=2][COLOR=#0000ff]While[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Not[/COLOR][/SIZE][SIZE=2] strA [/SIZE][SIZE=2][COLOR=#0000ff]Is[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Nothing
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] test() [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2] = strA.Split(","c)
[/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] test.GetValue(6) = " Yes" [/SIZE][SIZE=2][COLOR=#0000ff]Then
[/COLOR][/SIZE][SIZE=2]tmpPerson = [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] Person
tmpPerson.Firstname = test(0)
tmpPerson.Lastname = test(1)
MyPeople.Add(tmpPerson)
[/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]If
[/COLOR][/SIZE][SIZE=2]strA = sr.ReadLine()
DataGrid1.DataSource = MyPeople
[/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]While
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]If
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Sub
[/COLOR][/SIZE]
 
Its ok i fixed it, so now onto my last question for you all hopefully you can suggest the best method to do this. So all my data is stored in a sequential file which is fine ive got writing and reading working perfectly. Now i need functionality to edit and delete records. Whats the best way to go about doing that?
 
Oh!
So sorry...
I didn't realise you wanted to bind to a datagrid!
The arraylist won't work like this (You'd have to do quite a bit more to make a bindable list)

If you want to bind to a datagrid you would be best using a Datatable, I provided a link to a tutorial on datatables above.

Again very sorry!

Regards,

Andy
 
I'm confused why wouldnt it work? Seems to run fine on my end shows the data perfectly. I'm currently adding the rest of the array to make sure it all checks out but i think it works

Ok i ran it with all my data, it displays correctly except the columns are in the wrong order. Is there a way to fix that or do i need to use a datatable instead
 
Hi,

It will work but I imagine at the moment you're settings the datasource every time you add an item, and it's a bit dodgy.. if you follow this link you can see how it should be done, it's not too much work.

http://support.microsoft.com/kb/316302

As for editing the sequential file, it can be quite complicated, but I'll try to find a good link for you.
 
Yeh it works except it displays the columns in the wrong order, im trying to follow the microsoft code to find a way to force the column order

Ok I've looked through the code and i have no idea how to make them display in the order theyre declared. Is there any info I can provide or look at that would help work that out?
 
Its kind of late here so I need to get some sleep before work, but I'll check this page when i get up tmrw. Incase someone else stumbles across this post I've attached my code below.


VB.NET:
Public Class Person
Private _firstname As String
Private _lastname As String
Private _address As String
Private _phonenumber As String
Private _RSVP As String
Private _Guests As String
Private _RSVPDate As String
Private _Donation As String
Private _Thankyou As String
Private _isafriend As Boolean
Public Property Firstname() As String
Get
Return _firstname
End Get
Set(ByVal Value As String)
_firstname = Value
End Set
End Property
Public Property Lastname() As String
Get
Return _lastname
End Get
Set(ByVal Value As String)
_lastname = Value
End Set
End Property
Public Property Address() As String
Get
Return _address
End Get
Set(ByVal Value As String)
_address = Value
End Set
End Property
Public Property PhoneNumber() As String
Get
Return _phonenumber
End Get
Set(ByVal Value As String)
_phonenumber = Value
End Set
End Property
Public Property RSVP() As String
Get
Return _RSVP
End Get
Set(ByVal Value As String)
_RSVP = Value
End Set
End Property
Public Property Guests() As String
Get
Return _Guests
End Get
Set(ByVal Value As String)
_Guests = Value
End Set
End Property
Public Property RSVPDate() As String
Get
Return _RSVPDate
End Get
Set(ByVal Value As String)
_RSVPDate = Value
End Set
End Property
Public Property Donations() As String
Get
Return _Donation
End Get
Set(ByVal Value As String)
_Donation = Value
End Set
End Property
Public Property Thankyou() As String
Get
Return _Thankyou
End Get
Set(ByVal Value As String)
_Thankyou = Value
End Set
End Property
End Class

and section 2

VB.NET:
Private Sub test5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles test5.Click
Dim sr As IO.StreamReader
Dim tmpPerson As Person
cManager = CType(DataGrid1.BindingContext(MyPeople), CurrencyManager)
If IO.File.Exists("NEWRECORD.TXT") Then
sr = IO.File.OpenText("NEWRECORD.TXT")
Dim strA As String
strA = sr.ReadLine
While Not strA Is Nothing
Dim test() As String = strA.Split(","c)
If test.GetValue(7) = " Yes" Then
tmpPerson = New Person
tmpPerson.Lastname = test(0)
tmpPerson.Firstname = test(1)
tmpPerson.Address = test(2)
tmpPerson.PhoneNumber = test(3)
tmpPerson.RSVPDate = test(4)
tmpPerson.Thankyou = test(5)
tmpPerson.Donations = test(6)
tmpPerson.RSVP = test(7)
tmpPerson.Guests = test(8)
MyPeople.Add(tmpPerson)
End If
strA = sr.ReadLine()
End While
DataGrid1.DataSource = MyPeople
End If
End Sub

Also just to clarify the order it is displaying in is:

Donations - Lastname - RSVPDate - FirstName - Guests - Address - Phonenumber - Thankyou - RSVP
 
This should sort your columns...

VB.NET:
        Dim ts1 As New DataGridTableStyle
        ts1.MappingName = "Arraylist"

        Dim c1 As New DataGridTextBoxColumn
        c1.MappingName = "Firstname"
        c1.HeaderText = "Firstname"
        c1.Width = 100
        ts1.GridColumnStyles.Add(c1)

        Dim c2 As New DataGridTextBoxColumn
        c2.MappingName = "Lastname"
        c2.HeaderText = "Lastname"
        c2.Width = 100
        ts1.GridColumnStyles.Add(c2)

        DataGrid1.TableStyles.Add(ts1)

Place it in the form load event...
 
Back
Top