problem with propertymanager ( textbox bound to dataset )

P

Piotrekk

Hi

I have problem with propertymanager in my code. PositionChanged event
is not even raised. When i increase position, the value remains the
same - as a result textbox.Text value remains also unchanged.
How to solve these problems? I have tried to google but couldn't find
a solution.

ps - dataSet11 is typed dataset and is properly filled.

private void Form1_Load(object sender, EventArgs e)
{
employeesTableAdapter1.Fill(dataSet11.Employees);

tb = new TextBox();
this.Controls.Add(tb);
tb.Location = new Point(50, 50);
tb.DataBindings.Add("Text", dataSet11.Employees,
"Lastname");



pm = (PropertyManager)this.BindingContext[tb];
pm.PositionChanged += new
EventHandler(pm_PositionChanged);

}

void pm_PositionChanged(object sender, EventArgs e)
{

}

private void button1_Click(object sender, EventArgs e)
{
pm.Position += 1;
}

Kind Regards
 
M

Marc Gravell

I have problem with propertymanager in my code. PositionChanged event
is not even raised.

2 problems in the code; one "possible" and one "definite"; starting
with the latter:
pm = (PropertyManager)this.BindingContext[tb];

Here you are using "tb" as the data-source; to get the same currency-
manager you must bind to the same *data* - i.e.
this.BindingContext[dataSet11.Employees];

The second /possible/ issue is that the binding-context can actually
change during init; strictly speaking you would have to look at the
[On]BindingContextChanged override/event, otherwise it could a: error
(null value) or b: have a completely different binding-context
(unlikely, but theoretically possible).

However! If I can offer a simpler solution - use a BindingSource as an
intermediary?

BindingSource bs = new BindingSource();
bs.DataSource = dataSet11.Employees;
bs.PositionChanged += pm_PositionChanged;
tb.DataBindings.Add("Text", bs, "Lastname");

You can also use a BindingSource from the IDE designer.

Marc
 
P

Piotrekk

Thank you for your answer. But when is PropertyManager /
CurrencyManager really usefull then?
PK
 
M

Marc Gravell

But when is PropertyManager / CurrencyManager
Well, one typical case would be when you are writing a data-bound
control, such as a grid (or indeed, textbox); since you are now the
*consumer* of the bound data, you can only use the more abstract forms
that BindingContext gives you - specifically the concrete
BindingManagerBase subclasses: CurrencyManager if you are expecting a
list (i.e. a grid/list control), or PropertyManager if you expect a
single item at a time (i.e. TextBox etc).

There's nothing to stop you from using these as the *caller*, but
you'd still need to use it correctly ([On]BindingContextChanged etc) -
but in short you are making your life harder than necessary. As the
caller, you can get away with using BindingSource (or similar) to make
things simple.

Marc
 
P

Piotrekk

I guess I will stay with BindingSource for simplicity ( Since am quite
new to ADO.NET ) . Thank you.
 

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