Form BindingContext object indexer problem?

D

D. Yates

Hi,

I retrieved the employee table from the Pubs database into a single dataset
called, dataSet12. I dropped two textbox controls and a datagrid control
onto the same form and bound the controls at design time to dataSet12's
employee table. Now I want to move through the data by pressing a
button....

This code will change the data in the text boxes:
this.BindingContext[dataSet12, "employee"].Position += 1;

and this code will move the data in the datagrid:
this.BindingContext[dataSet12.employee].Position += 1;

If I do this, cm1 and cm2 are not equal:
CurrencyManager cm1 = (CurrencyManager)
this.BindingContext[dataSet12.employee];
CurrencyManager cm2 = (CurrencyManager) this.BindingContext[dataSet12,
"employee"];

if (cm1 == cm2)
cm1.Position++;

cm1 and cm2 are never equal. Why are they different?


I fixed my problem by changing the design property of the datagrid from
dataGrid1.DataSource = dataSet12.employee;
dataGrid1.DataMember = <blank>

to

dataGrid1.DataSource = dataSet12;
dataGrid1.DataMember = "employee";

and then this works:
this.BindingContext[dataSet12, "employee"].Position += 1;

Again, my question is why did I have to do this and why aren't these equal:
this.BindingContext[dataSet12, "employee"].Position += 1;
this.BindingContext[dataSet12.employee].Position += 1;


Personally, I would rather use:
this.BindingContext[dataSet12.employee].Position += 1;
since it doesn't require a internal table lookup. It is also the code shown
on page 328 of the MCAD/MCSD Self pace training kit for test 70-306/70-316.
The books example works because he is demonstrating runtime data binding and
he binds his text boxes with code that looks like this:
TextBox1.DataBindings.Add("Text", DataSet1.Customers, "CustomerID");

Dave
 
B

Bart Mermuys

Hi,

D. Yates said:
Hi,

I retrieved the employee table from the Pubs database into a single
dataset called, dataSet12. I dropped two textbox controls and a datagrid
control onto the same form and bound the controls at design time to
dataSet12's employee table. Now I want to move through the data by
pressing a button....

This code will change the data in the text boxes:
this.BindingContext[dataSet12, "employee"].Position += 1;

and this code will move the data in the datagrid:
this.BindingContext[dataSet12.employee].Position += 1;

If I do this, cm1 and cm2 are not equal:
CurrencyManager cm1 = (CurrencyManager)
this.BindingContext[dataSet12.employee];
CurrencyManager cm2 = (CurrencyManager) this.BindingContext[dataSet12,
"employee"];

if (cm1 == cm2)
cm1.Position++;

cm1 and cm2 are never equal. Why are they different?


I fixed my problem by changing the design property of the datagrid from
dataGrid1.DataSource = dataSet12.employee;
dataGrid1.DataMember = <blank>

to

dataGrid1.DataSource = dataSet12;
dataGrid1.DataMember = "employee";

and then this works:
this.BindingContext[dataSet12, "employee"].Position += 1;

Again, my question is why did I have to do this and why aren't these
equal:
this.BindingContext[dataSet12, "employee"].Position += 1;
this.BindingContext[dataSet12.employee].Position += 1;

Basicly that's how it is designed though it may be confusing. It's not
enough that both databindings end up with the same data, when you ask a
BindingContext for a CurrencyManager then you must use the exact same
DataSource and DataMember (excluding fieldname) then the ones you used for
the binding.

A BindingContext is basicly a map with DataSource+DataMember being the key
and BindingManagerBase(CurrencyManager or PropertyManager) being the value.
Each time you bind a list to some Control it needs a CurrencyManager to
maintain the position, so it checks whether the BindingContext already
contains a CurrencyManager for that DataSource+DataMember and if it does
then it uses the existing one, otherwise it creates a new CurrencyManager.
That should also explain why they can navigate together or independent.

So a combination of DataSource and DataMember(without fieldname) is the key
but there is _no_ resolving to see whether this points to the same data or
not.
Personally, I would rather use:
this.BindingContext[dataSet12.employee].Position += 1;
since it doesn't require a internal table lookup. It is also the code
shown on page 328 of the MCAD/MCSD Self pace training kit for test
70-306/70-316. The books example works because he is demonstrating runtime
data binding and he binds his text boxes with code that looks like this:
TextBox1.DataBindings.Add("Text", DataSet1.Customers, "CustomerID");

Yes, but if you use a typed DataSet then then the designer won't let you
bind TextBox's directly to a DataTable, it will always use the DataSet as
the DataSource.


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

Top