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 General / May 2006

Tip: Looking for answers? Try searching our database.

DataBinding, grr

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Mark Rendle - 07 Dec 2004 10:03 GMT
I hate DataBinding.

Using VS.NET 2003, C#, Sql Server database.

I've got a strongly-typed dataset. Let's pretend it's Northwind.

I've also got a TextBox.

If I set the databinding for the TextBox thus:

textBox1.DataBindings.Add("Text", northwindDataSet.Customers, "CompanyName");

then everything works beautifully.

If I set the databinding for the TextBox thus:

textBox1.DataBindings.Add("Text", northwindDataSet, "Customer.CompanyName");

it doesn't work at all. Which is a real shame, as that's the way the Form
Designer does it.

Anybody have any ideas what might be wrong?
Mark Rendle - 07 Dec 2004 10:19 GMT
Additional information:

This only happens on the form I've spent a week designing. I created a new
dummy form and passed the exact same instance of the DataSet through to it,
and both DataBinding methods work perfectly. So probably all I need to do is
throw away a week's work, start again, and hope that whatever gremlin got in
last time doesn't get in this time.

I really, really /HATE/ databinding.
Nigel Armstrong - 07 Dec 2004 12:27 GMT
Hi Mark

If you look at your code, in the second case you have Customer.CompanyName.
In the first case, your Table name is Customers.

See if Customers.CompanyName works in the second case....

HTH

Nigel Armstrong

> Additional information:
>
[quoted text clipped - 5 lines]
>
> I really, really /HATE/ databinding.
Mark Rendle - 07 Dec 2004 12:33 GMT
No, that's just because of the way the strongly-typed tables are named. The
actual DataTable name is "Customer", but the property which points to the
DataTable is called Customers.
Nigel Armstrong - 07 Dec 2004 17:23 GMT
Hi Mark

I just did this with Northwind. Drag the Customers table, generate a
DataSet, add to the designer - the usual stuff...

These are the two lines of code:

       Me.TextBox2.DataBindings.Add("Text", Me.NorthwindDataSet1.Customers,
"ContactName")
       Me.TextBox1.DataBindings.Add(New
System.Windows.Forms.Binding("Text", Me.NorthwindDataSet1,
"Customers.ContactName"))

(Sorry about the wrapping!)

Both work fine...

Nigel

> No, that's just because of the way the strongly-typed tables are named. The
> actual DataTable name is "Customer", but the property which points to the
> DataTable is called Customers.
Mark Rendle - 08 Dec 2004 11:23 GMT
Oh good. I'm _so_ glad it works the way it's _supposed_ to work for _you_.

Now, does anybody else have any ideas why it might _not_ be working for _me_?
Matt Berther - 09 Dec 2004 03:50 GMT
Hello Mark,

I've come to the realization that DataBinding, more often than not, is not
worth the headaches it provides.

However, if you would provide your code sample, Id be glad to try and help
you out.

--
Matt Berther
http://www.mattberther.com

> Oh good. I'm _so_ glad it works the way it's _supposed_ to work for
> _you_.
>
> Now, does anybody else have any ideas why it might _not_ be working
> for _me_?
Mark Rendle - 09 Dec 2004 13:37 GMT
Matt,

Thanks for the offer, but the actual problem is part of a huge application
with an associated database and everything, and when I try and isolate it
into a dummy app, the problem goes away.

I have found a work-around for the time being, involving looping through all
the Designer-created DataBindings and dynamically replacing them with the
working method.

I think I might just write my own DataBinding provider, though. It couldn't
be any worse.

Cheers,
Mark
Neil Allen - 09 Dec 2004 20:18 GMT
>I hate DataBinding.
>
[quoted text clipped - 18 lines]
>
>Anybody have any ideas what might be wrong?

Dear Mark

I understand that you are frustrated - so you have my sympathy. I hope
that the following isn't patronising or irritating. I apologise in
advance if this message adds to your frustration.

I'm not quite sure what you mean when you say that one set of
"DataBindings.Add" parameters work and another set don't.

I suspect that you might have a list control (DataGrid / ListBox etc)
or a CurrencyManager that deals with the navigation from one row to
another and that you wish to synchronise the Text property of a
TextBox with a specific column in the row in the list that the user or
program has navigated to.

As you probably know, one of the splendid things about databinding is
that you can have more than one control or currency manager displaying
a different "row" (or item) from the same "table" (or collection).

e.g. Configuring data binding for two DataGrids and two TextBoxes as
follows,,,

dataGrid1.DataSource = myCustomersDataset.Customers
dataGrid1.DataMember = ""
textBox1.DataBindings.Add(new System.Windows.Forms.Binding("Text",
myCustomersDataset.Customers, "CompanyName"))

dataGrid2.DataSource = myCustomersDataset
dataGrid2.DataMember = "Customers"
textBox2.DataBindings.Add(new System.Windows.Forms.Binding("Text",
this.myCustomersDataset, "Customers.CompanyName"))

... means that both DataGrids display the same Customer rows but they
navigate independently and textBox1 will keep in synch with dataGrid1
and textBox2 will keep in synch with dataGrid2.

On the other hand, such behaviour could be confusing if you expect
textBox2 to stay in synch with dataGrid1. And that's what I think
might have happened in your case. At least, that's what your example
DataBindings.Add parameters imply.

It is critical that the Binding.Add parameters for simple property /
property binding (such as TextBox.Text) match the complex binding
properties of list controls if you want to keep the list and simple
binding synchronised.

My simple minded, rule of thumb is that the DataSource property of the
DataGrid and the second parameter of the textbox's DataBindings.Add
method must refer to the same object. And, the string in the
datamember property of the DataGrid and the string (less any
characters to the right of the last full stop) of the third parameter
in  textbox's DataBindings.Add method must match. That sounds long
winded when I spell it out - but the rule is simple. (Although I
understand a bit more about what's going on underneath really).

As you've probably discovered - If you want to use the visual designer
to declare your data binding to a strongly typed DataSet component
then the DataGrid is the best suited list control to synchronise with
simple bound controls. That's mainly because the UI Type Editor thingy
that adds to the property binding to the TextBox in the visual
designer will generate the code in the format that matches the
DataGrid's DataSource / DataMember format in the DataGrid2 / TextBox2
example above.

Unfortunately, if you have no choice but to set a list control's
DataSource property using the "DataSetName.DataTablePropertyName"
format (as in the DataGrid1 / TextBox1 example above) then, I as far
as I know, you'll have to  write the code to add the simple binding
for TextBoxes (and the like).

This isn't a fault of databinding I think it's a limitation of the
visual designer / UI Type Editor.

Other possible reasons for "losing" the synchronisation of complex and
simple binding might include...

* The application programmer or a control (either by design or fault)
creates a new BindingContext.

* The original DataSet is replaced by a new instance *after* creating
the DataBindings. This results in the DataBinding infrastructure and
the DataSet variable referring to different objects.

* An exception is thrown in a bound property's property accessors. In
which case you can't catch the exception and the control's property
isn't updated but it keeps the focus.

I'm sorry this has been such a long winded and ill expressed message
but I hope it helps. Feel free to ask questions or to send me a sample
of the code that is causing you trouble.

Regards

Neil Allen
Mark Rendle - 10 Dec 2004 10:32 GMT
Neil,

That was so detailed, I feel really bad that it doesn't solve my problem.
Unfortunately, I'm not using any kind of complex binding; there's only one
row in the DataSet's master table, which is what the fields are binding to.
But I'll check and see whether there's something weird going on with
BindingContexts anywhere. (I'm a little bit calmer after working on something
else for a couple of days!)

Thanks for the response.

Mark
Mina Nagy - 21 May 2006 00:38 GMT
Hi Mark,
I think I'm having a similar problem. In my nightmare project I have a
dataset with three tables (a parent and two child tables). The parent table
has only one record added at run time.
On the form there exist two combo boxes each is bound to one child table.
The problem is that I get an error that says "An unhandled exception of type
'System.ArgumentOutOfRangeException' occurred in system.dll
Additional information: Specified argument was out of the range of valid
values."

I tried to set the databindings both by designer and programatically but
always the same error.

The most interesting thing is that when I tried to change the values of the
lonely record of the parent table I get some strange behavior of the
form(i.e. the combo box that I change its value - at run time - doesn't want
to lose focus).

Signature

Professor Mina
professor_102@hotmail.com

> Neil,
>
[quoted text clipped - 8 lines]
>
> Mark
Mark Rendle - 21 May 2006 11:34 GMT
Hi Mina

I've found that things have got a lot better with the new BindingSource
component in .NET 2.0. Maybe you could upgrade?

Mark

> Hi Mark,
> I think I'm having a similar problem. In my nightmare project I have a
[quoted text clipped - 13 lines]
> form(i.e. the combo box that I change its value - at run time - doesn't want
> to lose focus).
Kevin Spencer - 21 May 2006 16:13 GMT
The BindingSource component is excellent!

Signature

HTH,

Kevin Spencer
Microsoft MVP
Professional Numbskull

The man who questions opinions is wise.
The man who quarrels with facts is a fool.

> Hi Mina
>
[quoted text clipped - 24 lines]
>> want
>> to lose focus).
Mina Nagy - 22 May 2006 01:39 GMT
Ok Kevin, IT IS PERFECT but what's wrong with my app then? can you tell me?
Signature

Professor Mina
professor_102@hotmail.com

> The BindingSource component is excellent!
Kevin Spencer - 22 May 2006 11:29 GMT
I cannot. You have not provided enough information for diagnosis.

Signature

HTH,

Kevin Spencer
Microsoft MVP
Professional Numbskull

The man who questions opinions is wise.
The man who quarrels with facts is a fool.

> Ok Kevin, IT IS PERFECT but what's wrong with my app then? can you tell
> me?
>
>> The BindingSource component is excellent!

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.