Click here to Skip to main content
15,881,938 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
C#, VS-2010, winforms

Generally I want to create user control with a bindable MyProperty. But I met some problem: Data row become modified when current row changed.

You can download C# project and try it yourself:
test_bind.zip



To make it simple I did follow:
1. create test form with a
* dataGridView (set it ReadOnly),
* TextBox,
* some typed DataSet with some Table
* bindingSource
2. bind bindingSource to the table
3. bind dgv to bindingSource
4. bind TextBox.Text to bindingSource to some text column ("name")
5. on form load fill table with some data. (If you add rows manually do not forget AcceptChanges())
6. add event handler for table xxx_ColumnChanging(...) and set breakpoint there

all standard steps, nothing special

Run

as expected - rows in dgv, current name in textBox, click on different rows in grid... and only when I modify text in textbox I stop in breakpoint.

Lets modify program, and bind textBox to different property instead of Text - lets say Tag. Set DataSourceUpdateProperty "OnValidation" or "OnPropertyChanged".

Run

Now current row become modified. !!!

Same happens when instead of textBox I use UserControl with a dummy property
String MyProperty { set; get; }

How to bind custom property in user control with the same behaviour as TextBox.Text?

Note:
Binding property itself is not a problem. Problem is: when I bind to standard TextBox.Text property - everything Ok Wen I bind to ANY OTHER property (Tag) or to custom Property in User Control - then DATASOURCE ALWAYS BECOME MODIFIED FOR CURRENT ROW
Posted
Updated 11-Sep-12 7:25am
v7
Comments
Kenneth Haugland 4-Sep-12 16:50pm    
Would definatley use a DependencyProperty in WPF, but winforms... Binding perhaps?

 
Share this answer
 
Comments
parfilko 6-Sep-12 12:01pm    
unfortunately, your example just repeat functionality of standard BindingSource component.
If form has 50 controls it will need to add to many user code. With a BindingSource component there will be NO user code at all.
I prefer use standard BindingSource designed by microsoft, rather than custom code.

As I mentioned, BindingSource component works perfect while I use TextBox.Text property - it synchronize data two way and update source only when Text property is really changed by control or by programmer code.

MSDN show how to create own binding property (on user control):
http://msdn.microsoft.com/en-us/library/ms171926.aspx
When I did that, it's works BUT UPDATES DATASOURCE EVERYTIME, even control does not change the data. This is unexpected behavior. Or Bug?
Short answer is:

When bind to simple property, declared like

[Bindable(true, BindingDirection.TwoWay)]           //  I need TwoWay    
public String MyProp {set;get;}


then datasource will be updated on current position change. You may count it as Bug or Feature...

To change behavior - I need magic word. Declare event in the control as

public event System.ComponentModel.PropertyChangedEventHandler MyPropChanged;


This name is special: "YourPropertyName" + "Changed" = YourPropertyNameChanged
Other word will not be magic.
As soon as event declared - datasource would not be updated every time as before...

and now I can control the process...
public void OnMyPropChanged(System.ComponentModel.PropertyChangedEventArgs e)
{
    Binding bb = this.DataBindings[0] as Binding;
    bb.WriteValue();
    if (MyPropChanged != null) MyPropChanged(this, e);
}

private void TxtChanged(object sender, EventArgs e)
{
    if (load) { return; }
    _my_prop = Txt.Text;   // ...
    OnValueTwoChanged(new PropertyChangedEventArgs("MyProp"));
}


Here is the example project with more details, where I create user control with String property ValueTwo
which split in two different TextBoxes using some custom logic, and combine value and update when user change the text.


test_bind_fixed.zip
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900