Click here to Skip to main content
Rate this: bad
Please Sign up or sign in to vote.
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:

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
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".
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?
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 4-Sep-12 8:51am
Edited 11-Sep-12 8:25am
Kenneth Haugland at 4-Sep-12 16:50pm
Would definatley use a DependencyProperty in WPF, but winforms... Binding perhaps?
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

parfilko at 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):
When I did that, it's works BUT UPDATES DATASOURCE EVERYTIME, even control does not change the data. This is unexpected behavior. Or Bug?
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

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;
        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.

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 9,535
1 OriginalGriff 8,295
2 Peter Leow 4,839
3 Kornfeld Eliyahu Peter 3,210
4 Maciej Los 2,301

Advertise | Privacy | Mobile
Web03 | 2.8.150327.1 | Last Updated 11 Sep 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100