how to do Binding with code

M

Marc R.

Hi all,
The beginner strikes back again (sorry)

this is the class I use for Nodes into a treeview,
I need to bind each field to a database..... How Can I do that ?

What I tried so far is to run a Dataview for operations, then set the
DataView rowfilter to "ser_id = " & ctype(nodes, service).id
then change directly into the Dataview.item(0) the columns I need to change.
Is there a more simple way to bind the "Service" nodes directly to the table
so I don't have to work with the Dataview ?

Any Example how to build a binding to a database with our own class would be
appreciated.

Thanks to you all,
Have a nice day !

Marc R.






Public Class service
Inherits treenodes

'Private Member

Private p_ID As Integer

Private p_Name As String

Private P_Active As Boolean

Public Property ID() As Integer

Get

Return p_ID

End Get

Set(ByVal Value As Integer)

p_ID = Value

End Set

End Property

Public Property Name() As String

Get

Return p_Name

End Get

Set(ByVal Value As String)

p_Name = Value

End Set

End Property

Public Property Active() As Boolean

Get

Return P_Active

End Get

Set(ByVal Value As Boolean)

P_Active = Value

End Set

End Property

End Class
 
M

Marc R.

Marc R. said:
Hi all,
The beginner strikes back again (sorry)

this is the class I use for Nodes into a treeview,
I need to bind each field to a database..... How Can I do that ?

What I tried so far is to run a Dataview for operations, then set the
DataView rowfilter to "ser_id = " & ctype(nodes, service).id
then change directly into the Dataview.item(0) the columns I need to
change. Is there a more simple way to bind the "Service" nodes directly to
the table so I don't have to work with the Dataview ?

Any Example how to build a binding to a database with our own class would
be appreciated. Actually, I would like to bind each service to 1 datarow
only

Thanks to you all,
Have a nice day !

Marc R.






Public Class service
Inherits treenodes

'Private Member

Private p_ID As Integer

Private p_Name As String

Private P_Active As Boolean

Public Property ID() As Integer

Get

Return p_ID

End Get

Set(ByVal Value As Integer)

p_ID = Value

End Set

End Property

Public Property Name() As String

Get

Return p_Name

End Get

Set(ByVal Value As String)

p_Name = Value

End Set

End Property

Public Property Active() As Boolean

Get

Return P_Active

End Get

Set(ByVal Value As Boolean)

P_Active = Value

End Set

End Property

End Class
 
P

Phill W.

Is there a more simple way to bind the "Service" nodes directly to the
table so I don't have to work with the Dataview ?

Certainly, but you just wind up with the code inside the Service class,
rather than anywhere else and you'll be starting to fight the battles of
encapsulation vs performance.

Some suggestions:

1) Leave things as they are, loading data into each Service object
from the DataView - OK, it's "messy", but it works.
At the very least, I recommend to code your own Constructor
so that you set all the properties in one go, as in

Public Class Service
. . .
Public Sub New(
ByVal id As Integer _
, ByVal name As String _
, ByVal active As Boolean _
)
' Load values into properties
p_ID = id
p_Name = name
p_Active = active

End Sub
.. . .

2) Keep the DataView (so your database access stays where it is),
and create each Service object based on, say, a row from the
DataView, something like

Public Class Service
Public Sub New( _
ByVal sourceData as DataViewRow _
)
' Load data from DataViewRow into properties
p_ID = sourceData.Item( "id" )
End Sub
.. . .

Both of the above are limited in that you're getting a /copy/ of the data
in each object. unless you move the database code itself into the Service
class, you can't feed changes [directly] back into the database, so ...

3) Transfer all the data access into the Service class as well.
You'd probably want to do much the same as in (2), but passing
in the database /connection/ instead of the already loaded DataView,
and probably the id of the Service to load.

Public Class Service
Public Sub New( _
ByVal id As Integer _
, ByVal connection As WhicheverConnectionYouUse _
)
' Save the connection, so we can use it do updates - maybe
p_Connection = connection

' Load data from DataViewRow into properties
p_ID = GoAndGetDataFromConnection( connection, id )

End Sub
.. . .

4) Or, because that'll be /horrendously/ slow cmpared to loading the
data from the DataView (because each Service object has to do its
own, /individual/ data retrieval), you might tackle it completely the
other way around ... Create a Shared method on the Service class
that takes a /TreeView/ as argument, finds all the Services (from the
Database), creates a Service object for each and adds these into the
TreeView. <whew>

Public Class Service
Public Shared Sub LoadTree( _
ByVal treeToLoad As TreeView _
, ByVal connection As WhicheverConnectionYouUse _
)
Dim dv As DataView = ... DataFromDatabase

For Each dr As DataRowView In dv
Dim node As Service _
= New Service( dr )

treeToLoad.Nodes.Add( node )
Next
End Sub
.. . .

(plus, of course, at least 101 others I /haven't/ thought of ... )

Personally, I think I'd be heading for option (2).

HTH,
Phill W.
 
M

Marc R.

the funny part I use suggestion #1 and 2 already this is the way I was
planning to go.
At lease now I know I'm on the good track

Thanks.


Phill W. said:
Is there a more simple way to bind the "Service" nodes directly to the
table so I don't have to work with the Dataview ?

Certainly, but you just wind up with the code inside the Service class,
rather than anywhere else and you'll be starting to fight the battles of
encapsulation vs performance.

Some suggestions:

1) Leave things as they are, loading data into each Service object
from the DataView - OK, it's "messy", but it works.
At the very least, I recommend to code your own Constructor
so that you set all the properties in one go, as in

Public Class Service
. . .
Public Sub New(
ByVal id As Integer _
, ByVal name As String _
, ByVal active As Boolean _
)
' Load values into properties
p_ID = id
p_Name = name
p_Active = active

End Sub
. . .

2) Keep the DataView (so your database access stays where it is),
and create each Service object based on, say, a row from the
DataView, something like

Public Class Service
Public Sub New( _
ByVal sourceData as DataViewRow _
)
' Load data from DataViewRow into properties
p_ID = sourceData.Item( "id" )
End Sub
. . .

Both of the above are limited in that you're getting a /copy/ of the data
in each object. unless you move the database code itself into the Service
class, you can't feed changes [directly] back into the database, so ...

3) Transfer all the data access into the Service class as well.
You'd probably want to do much the same as in (2), but passing
in the database /connection/ instead of the already loaded DataView,
and probably the id of the Service to load.

Public Class Service
Public Sub New( _
ByVal id As Integer _
, ByVal connection As WhicheverConnectionYouUse _
)
' Save the connection, so we can use it do updates - maybe
p_Connection = connection

' Load data from DataViewRow into properties
p_ID = GoAndGetDataFromConnection( connection, id )

End Sub
. . .

4) Or, because that'll be /horrendously/ slow cmpared to loading the
data from the DataView (because each Service object has to do its
own, /individual/ data retrieval), you might tackle it completely the
other way around ... Create a Shared method on the Service class
that takes a /TreeView/ as argument, finds all the Services (from the
Database), creates a Service object for each and adds these into the
TreeView. <whew>

Public Class Service
Public Shared Sub LoadTree( _
ByVal treeToLoad As TreeView _
, ByVal connection As WhicheverConnectionYouUse _
)
Dim dv As DataView = ... DataFromDatabase

For Each dr As DataRowView In dv
Dim node As Service _
= New Service( dr )

treeToLoad.Nodes.Add( node )
Next
End Sub
. . .

(plus, of course, at least 101 others I /haven't/ thought of ... )

Personally, I think I'd be heading for option (2).

HTH,
Phill W.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top