Databinding

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Can someone explain to me the difference between these two bindings?

dim b as Binding

b=New Binding(dsMyDataset.tblMyTable, "colMyColumn")

b=New Binding(dsMyDataset, "tblMyTable.colMyColumn")

They produce different behavior in my application. I know which one I must
use, but I don't understand why.

Thanks for the help.
 
This is the cool thing about Winforms databinding ;-). Currency managers are
what controls your binding to datasources but depending on the *path* you
take to that datasource you will end up with different currency manager
references, hence, different views into your data. This allows you to decide
whether controls should be syncronized or not to the same exact source
object. So for example if we set up controls like this:

Me.TextBox1.DataBindings.Add("Text", MyDataset, "Customers.CustomerID")
Me.TextBox2.DataBindings.Add("Text", MyDataset, "Customers.CustomerName")

You end up with both controls bound through the same Currency manager. You
can see this by taking a look at the Bindings collection:

Dim CM As CurrencyManager = DirectCast(Me.BindingContext(MyDataset,
"Customers"), CurrencyManager)
Dim bindingsCount As Integer = CM.Bindings.Count '-- returns 2

Now if we did this:

Me.TextBox1.DataBindings.Add("Text", MyDataset, "Customers.CustomerID")
Me.TextBox2.DataBindings.Add("Text", MyDataset.Tables("Customers"),
"CustomerName")

In this case you will end up with two completely separate Currency managers
because the *path* to your data source is different even though both
controls will be editing the same table, Customers.

Dim CM1 As CurrencyManager = DirectCast(Me.BindingContext(MyDataset,
"Customers"), CurrencyManager)
Dim bindingsCount1 As Integer = CM1.Bindings.Count '-- returns 1

Dim CM2 As CurrencyManager =
DirectCast(Me.BindingContext(MyDataset.Tables("Customers")),
CurrencyManager)
Dim bindingsCount2 As Integer = CM2.Bindings.Count '-- returns 1

The a currency manager maintains a view of the datasource so you can easily
access the current DataView as well as the current DataRowView:

Dim dv As DataView = DirectCast(CM.List, DataView)
Dim dvr As DataRowView = DirectCast(CM.Current, DataRowView)

You can obtain currency managers for any table/path in your dataset even if
there are no control bindings set (in that case the Bindings.Count would be
0). Complex winforms databinding can take some practice, but once you get
the hang of it you can create some very cool forms.

-B
 
Yes, I agree that the ability to create multiple currency managers against
the same DataSource is a powerful tool (I am only now getting into it in my
application; and the doors it opens are very interesting); however, this
implementation smells a little like an unintended feature. As I read your
response, your ability to create these independent CurrencyManagers is
limited by the number of unique paths to the same data element you can come
up with. It seems like just an explicit declaration of "New
CurrencyManager..." would be better.

As I said, I'm just now getting into this, so maybe I'm just not fully
appreciating the situation yet.

Thanks, though, for the explanation.

Pat
 
I didn't mention it in my first post, but you can create as many cm's to the
datasources as you want by creating different DataView references and bind
to them instead. So it isn't limited.

Dim dv1 As New DataView(myDataSet.Table1)
Dim dv2 As New DataView(myDataSet.Table1)
Dim dv3 As New DataView(myDataSet.Table1)
Dim dv4 As New DataView(myDataSet.Table1)
Dim dv5 As New DataView(myDataSet.Table1)
'-- These will be different currency managers
Dim cm1 As CurrencyManager = DirectCast(Me.BindingContext(dv1),
CurrencyManager)
Dim cm2 As CurrencyManager = DirectCast(Me.BindingContext(dv2),
CurrencyManager)
Dim cm3 As CurrencyManager = DirectCast(Me.BindingContext(dv3),
CurrencyManager)
Dim cm4 As CurrencyManager = DirectCast(Me.BindingContext(dv4),
CurrencyManager)
Dim cm5 As CurrencyManager = DirectCast(Me.BindingContext(dv5),
CurrencyManager)
 
Pat,

I was in your first message not sure what binding you where talking about,
because I know only a binding with three parameters and you was using two.

When you removed the first parameter, than you are in my opinion writing.

New Binding("x", Datatable(tblMyTable), "colMyColumn"
and in the second
New Binding ("x",Dataset, the column dolMyColumn from the
DatatTable(tblMyTable)

What is the different behaviour you get, before I start endless searching
for that, as most people am I almost forever using the same method, in my
case the first one?

Cor
 
Sorry Cor,

You're right. For the sake of discussion, let's say the first parameter is
"Text".

As for the different behavior, well, once Beth enlightened me on the
"different path, different currency manager" thing, it became... well not
CLEAR, but not completely opaque either.

I will try to distill my problem. The following code throws no exceptions;
mds.Tables(0).Rows.Count <> 0, yet CMA.Count=0. How can I change the order
of execution or the definition of CMA to ensure that
CMA.Count=mds.Tables(0).Rows.Count?


Private mds As DataSet
Private CMA as CurrencyManager

Protected Overrides Sub OnParentRowChanged()
Dim entID As Long
Dim entType As Integer
Dim b As Binding
Dim blnReBind As Boolean = False
Dim strBind As String = ""

Select Case MyBase.ParentRow.GetType.Name
Case "tblItemRow"
mds = global.dsItemContext1
entID = ParentRow("ItemID")
entType = 1
If Not CMA Is Nothing Then
If Not CType(CMA.Current, DataRowView).Row.GetType.Name
= "tblItemRow" Then
blnReBind = True
End If
Else
blnReBind = True
End If
strBind = "tblItem.tblItem"
Case "tblSystemRow"
mds = global.dsSystemContext1
entID = ParentRow("SystemID")
entType = 0
If Not CMA Is Nothing Then
If Not CType(CMA.Current, DataRowView).Row.GetType.Name
= "tblSystemRow" Then
blnReBind = True
End If
Else
blnReBind = True
End If
strBind = "tblSystem.tblSystem"
End Select
mds.Tables("tblMaintenanceScheduleDetail").Clear()
mds.Tables("tblMaintenanceSchedule").Clear()
If blnReBind Then
Me.combo0.DataBindings.Clear()
Me.combo0.DataBindings.Add(New Binding("SelectedValue", mds,
strBind & "tblMaintenanceSchedule.Activity"))
b = New Binding("CheckState", mds, strBind &
"tblMaintenanceSchedule.WarrantyRequirement")
AddHandler b.Format, AddressOf BooleanToCheckState
AddHandler b.Parse, AddressOf CheckStateToBoolean
Me.chk0.DataBindings.Clear()
Me.chk0.DataBindings.Add(b)
CMA = DirectCast(Me.BindingContext(mds, strBind &
"tblMaintenanceSchedule"), CurrencyManager)
blnReBind = False
End If
LoadData(entID, entType)
SourceListBox(entID, entType)
End Sub

Protected Overloads Sub LoadData(ByVal entityID As Long, ByVal
entityType As Integer)
Dim dsTemp As New DataSet

dsTemp.Merge(global.DAC.GetMaintenanceSchedules(, entityType,
entityID))
If Not dsTemp.Tables(0).Rows.Count = 0 Then
mds.Merge(dsTemp)
End If
End Sub
 
Pat,

I did not real test your code however this looks on first sight strange

End Select
mds.Tables("tblMaintenanceScheduleDetail").Clear()
mds.Tables("tblMaintenanceSchedule").Clear()
***Here you are cleaning the tables which you after that in my opinion want
to bind

If blnReBind Then
Me.combo0.DataBindings.Clear()
Me.combo0.DataBindings.Add(New Binding("SelectedValue", mds,
strBind & "tblMaintenanceSchedule.Activity"))
b = New Binding("CheckState", mds, strBind &
"tblMaintenanceSchedule.WarrantyRequirement")

In this case would I make a simple extra string value for the thirth
paramater between that which you can debug. I have now the idea that here
something is not going right,

Just my idea

Cor
 
Hi Cor,

Thanks for the response. Yes, the 2 lines in question don't belong there,
they are vestiges of another solution (since I couldn't get this to work, I
started clearing the dataset and loading it only with the relevant records).

Regarding strBind, I believe I need it because I don't know what context I
will need to bind to until run-time, although I DO know that the dataset to
which the controls will be bound contains the tables "tblMaintenanceSchedule"
and "tblMaintenanceScheduleDetail". Without strBind (or something like it),
the controls wouldn't synchronize with their parent (System or Item as the
case may be at run-time).

Any suggestions would be appreciated.

Thanks again,

Pat
 
Pat,

The trouble is that with seeing this code I don't know as I earlier asked
what goes wrong and as well not what you want to achieve.

The second is that it is about a strongly typed dataset, what I never use
and therefore for me a little bit extra difficult to understand what goes
wrong.

Therefore tell me first a little bit what you want.

Probably you get no answer more today, I have to go however maybe somebody
else sees it than..

Cor
 

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

Back
Top