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 / .NET Framework / General / January 2007

Tip: Looking for answers? Try searching our database.

any tips for trouble shooting databinding?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Jeremy Chaney - 30 Jan 2007 01:58 GMT
I'm writing an application in C# and I've used the IDE to bind a
checkbox to a boolean in my app. The properties for my checkbox in the
Visual Designer indicate that my databinding for the "Checked" state is
correct, but when I run my app, my button does not update to reflect the
state of the property I've bound it to. I've stepped through the code
and know that my data value is changing, but the UI does not reflect the
change.

Any ideas how to trouble shoot this? I've spent the last hour searching
Google and Google Groups, but I haven't found anything.

Thanks,
--Jeremy
PhilipDaniels@foo.com - 30 Jan 2007 09:35 GMT
>I'm writing an application in C# and I've used the IDE to bind a
>checkbox to a boolean in my app. The properties for my checkbox in the
[quoted text clipped - 9 lines]
>Thanks,
>--Jeremy

It's probably that the control is not aware that the boolean has
changed. Where is the boolean: in a DataSet, a BindingList<> or a
single instance of your own business object?

--
Philip Daniels
Jeremy Chaney - 30 Jan 2007 17:20 GMT
>> I'm writing an application in C# and I've used the IDE to bind a
>> checkbox to a boolean in my app. The properties for my checkbox in the
[quoted text clipped - 16 lines]
> --
> Philip Daniels

It is in a property in one of my classes:

private bool m_bMyBool;
public bool MyBool
{
    get { return m_bMyBool; }
    set { m_bMyBool = value; }
}

Thanks,
--Jeremy
PhilipDaniels@foo.com - 30 Jan 2007 20:28 GMT
>> It's probably that the control is not aware that the boolean has
>> changed. Where is the boolean: in a DataSet, a BindingList<> or a
[quoted text clipped - 12 lines]
>Thanks,
>--Jeremy

I think my initial suspicion was correct, though it's difficult to be
absolutely sure without seeing your app. Basically, at the moment the
bound control is ignorant of any change in the object. To make it
aware you need to tell it something has changed, which you do by
implementing the INotifyPropertyChanged interface on your object. From
the Visual Studio help:

<quote>
"The INotifyPropertyChanged interface is used to notify clients,
typically binding clients, that a property value has changed.

For example, consider a Person object with a property called
FirstName. To provide generic property-change notification, the Person
type implements the INotifyPropertyChanged interface and raises a
PropertyChanged event when FirstName is changed. If your data source
implements the INotifyPropertyChanged and you are performing
asynchronous operations you should not make changes to the data source
on a background thread. Instead, you should read the data on a
background thread and merge the data into a list on the UI thread.

For change notification to occur in a binding between a bound client
and a data source, your bound type should either:

Implement the INotifyPropertyChanged interface (preferred).

Provide a change event for each property of the bound type.

Do not do both."
</quote>

There is example code too.

To be honest, I have never bothered with implementing this interface
because my objects always need to live within some sort of collection.
If you make this a specialization of the BindingList generic type:

public class ListOfMyBools : BindingList<MyBool> { }

Then you can get all the 2 way binding "for free" on any type of
object. BindingList broadcasts changes at item level rather than
item.property level (see page 650 of "Windows Forms 2.0 Programming"
by Chris Sells and Michael Weinhardt).

If you really want to understand data binding then I recommend the
book "Data Binding With Windows Forms 2.0" by Brian Moyes, published
by Addison Wesley.

--
Philip Daniels
Jeremy Chaney - 31 Jan 2007 01:14 GMT
>>> It's probably that the control is not aware that the boolean has
>>> changed. Where is the boolean: in a DataSet, a BindingList<> or a
[quoted text clipped - 61 lines]
> --
> Philip Daniels

This is great info. I'll give it a try, and I'll check out the book you
recommend. Thank you.
--Jeremy

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.