Uninitialized data-binding

  • Thread starter Thread starter Viraptor
  • Start date Start date
V

Viraptor

Hello
I've created a form with 2 listboxes. Both have data-binding. First one
is "normal", 1:1 view of table. Second is filled using parameter from
the first one.

this.xxxTableAdapter.Fill(this.mainDataSet.XXX,
(int)this.BindingContext[yyyBindingSource, "id"].Current);

When I do it in Form_Load() it shows first listbox with element with
"id" of value 2 selected, but this.BindingContext[yyyBindingSource,
"id"].Current == 1. When I do the same thing in
yyyBindingSource_CurrentChanged later it gives expected results.
How can I fix the behaviour just after window creation? I guess it's
connected with listbox not initialized properly with new data between
those two .Fill() calls.
 
Message passed unnoticed. So once more:
After some tests i found out that in function
setsBindingSource_CurrentChanged() value:
(DataRowView)setsBindingSource.Current)["id"]
is correct, but
BindingContext[setsBindingSource, "id"].Current
shows the previous value, not the new one. Is that normal? Is there any
other/better way to get that "id"?
 
Hi,

Viraptor said:
Message passed unnoticed. So once more:
After some tests i found out that in function
setsBindingSource_CurrentChanged() value:
(DataRowView)setsBindingSource.Current)["id"]
is correct, but

That's the correct way. A BindingSource owns a CurrencyManager which manages
a list binding and current position.

Calling:
setsBindingSource.Current

results in:
setsBindingSource.CurrencyManager.Current.
BindingContext[setsBindingSource, "id"].Current
shows the previous value, not the new one. Is that normal?

This is very confusing. In NET1.1 there were no BindingSource's, there was
a CurrencyManager and BindingContext. BindingContext used to own
CurrencyManagers.

But in NET2.0 a BindingSource directly owns a single CurrencyManager, so if
you pass a BindingSource to the indexer of any BindingContext to request a
CurrencyManager it will relay the request back to the BindingSource passed
in:

If you pass setBindingsSource to the indexer of *any* BindingContext:
BindingContext[setBindingsSource , "id"]

it will return:
setBindingsSource.GetRelatedCurrencyManager("id")

if DataMember is empty then it will return:
setBindingSource.CurrencyManager

but since DataMember isn't empty it willl create a new BindingSource:
newBindingSource.DataSource = setBindingsSource
newBindingSource.DataMember = "id"
return newBindingSource.CurrencyManager

This new BindingSource/CurrencyManager is linked with the original. When
the original changes this new one will change, usually this is used for
master-child scenarios where a field can return a child list. But since id
returns a simple integer it simply uses that as current.

So in the end you are creating an additional BindingSource/CurrencyManager
for nothing though this additional BindingSource/CurrencyManager is synced
with the original one.

Is there any
other/better way to get that "id"?

The first way you showed is good:
((DataRowView)setsBindingSource.Current)["id"]

Or a more generic way:
setBindingSource.GetItemProperties(null).Find("id",false).GetValue(setBindingSource.Current)

HTH,
Greetings
 

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