Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm Data Binding / November 2005

Tip: Looking for answers? Try searching our database.

Does Adding More Than 1 Binding Work in VS2005?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
gary59@chartermi.net - 29 Nov 2005 01:18 GMT
If I bind multiple properties of a single control to different sources,
only the first one ever works. For example, I subclass a TextBox and
add an IsLocked property. I then add bindings for the Text property and
my new IsLocked property, but only the first property will ever be
bound. I can reverse the code, and again the first one works. I'm
reluctant to consider the bug is anywhere but mine, but I'm running out
of ideas...

example:
' (IsLocked works)
           Me.DataBindings.Add("IsLocked", drSource, "IsLocked")
           Me.DataBindings.Add("Text", drSource, "UserName")

'(Text works):
          Me.DataBindings.Add("Text", drSource, "UserName")
          Me.DataBindings.Add("IsLocked", drSource, "IsLocked")

Gary B
Bart Mermuys - 29 Nov 2005 15:23 GMT
Hi,

> If I bind multiple properties of a single control to different sources,
> only the first one ever works. For example, I subclass a TextBox and
[quoted text clipped - 12 lines]
>           Me.DataBindings.Add("Text", drSource, "UserName")
>           Me.DataBindings.Add("IsLocked", drSource, "IsLocked")

I've tried a basic example with DataBinding both the Text and ReadOnly
property of a TextBox.

DataTable dt =  new DataTable();
dt.Columns.Add( "Desc", typeof(string));
dt.Columns.Add( "DescRO", typeof(bool));
dt.Rows.Add(new object[] { "test1", true } );
dt.Rows.Add(new object[] { "test2", false } );

dataGridView1.DataSource = dt;  // used grid to select a row
textBox1.DataBindings.Add("Text", dt, "Desc");
textBox1.DataBindings.Add("ReadOnly", dt, "DescRO");

This code seems to work fine, can you post all code needed to reproduce the
problem ?

HTH,
Greetings

> Gary B
gary59@chartermi.net - 30 Nov 2005 01:32 GMT
Thanks for your reply Bart,
I'm in the process of trying that, and it's still not quite right...The
textbox tracks the datasource ok, but the datasource doesn't track the
textbox changes. In your example above, you can change the text and the
datatable will change, but if you change the ReadOnly flag (you have to
do that programmatically), the datatable will only be changed if you
change the text and lose focus on the textbox. So underneath it all,
the textbox control is checking (on the LostFocus event it appears) and
only changes the bound source data if the text property changed. But in
my case, there may be other properties of the dataset that are changed
irrespective of the text. I guess I need to do whatever LostFocus is
doing 'under the hood' whenever another property changes so that the
datasource is updated. But that seems like a lot of extra wiring I
shouldn't have to do...I mean it works correctly for the text property.
gary B
Bart Mermuys - 30 Nov 2005 02:42 GMT
Hi,

> Thanks for your reply Bart,
> I'm in the process of trying that, and it's still not quite right...The
[quoted text clipped - 11 lines]
> shouldn't have to do...I mean it works correctly for the text property.
> gary B

When a property changes ( eg. TextChanged/ ReadOnlyChanged ) then the
change(s) aren't persisted back to the DataSource until the Control fires
the Validating event.  This is normal behaviour like it was also in NET1.1.

The above does match your description for the Text property (which isn't
changed from code).  But for the ReadOnly property that is changed from
code, i wonder whether you are changing it from a button event handler,
because in that case TextBox.Validating is fired before the ReadOnly
property is changed, so instead of changing the Text you need to go back to
the TextBox and then leave it again to get another Validating event.

Now there are a couple of ways to alter this behaviour:

1) You can force persisting changed properties back into the DataSource, by
using EndCurrentEdit, it's important to know that only the properties that
have changed will be persisted (unless they don't have a Changed event).

// DataSource and DataMember need to be the same as the ones used for the
Binding.
// DataMember must not include any fieldname/propertyname and can be empty
string
// in which case it may be omitted.
BindingContext(DataSource, DataMember).EndCurrentEdit()
or
BindingContext(DataSource).EndCurrentEdit()

The above will cause something similar to the Validating event.

2) In NET2.0 there is a way to make properties persist back to the
DataSource when the property is changed (*), instead of the default when the
control is Validating:

textBox1.DataBindings.Add("Text", dt, "Desc", false,
DataSourceUpdateMode.OnPropertyChanged);
textBox1.DataBindings.Add("ReadOnly", dt, "DescRO", false,
DataSourceUpdateMode.OnPropertyChanged);

(*) If you add  a property to a Control, then i advise you to add a
<PropertyName>Changed event and fire it when the property has changed.

Public Class MyControl
 Inherits TextBox
 Private mSomeProp As Boolean

 Public Event SomePropChanged As EventHandler

 Public Property SomeProp As Boolean
 Get
   return mSomeProp
 End Get
 Set ( Value As Boolean )
   If ( Value <> mSomeProp )
      mSomeProp = Value
      RaiseEvent SomePropChanged(Me, EventArgs.Empty)
    End If
  End Set
  End Property
End Class

HTH,
Greetings

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.