views:

1030

answers:

3

I'm currently using winforms databinding to wire up a data editing form. I'm using the netTiers framework through CodeSmith to generate my data objects. For database fields that allow nulls it creates nullable types. I've found that using winforms databinding the controls won't bind properly to nullable types.

I've seen solutions online suggesting that people create new textbox classes that can handle the nullable types but that could be a pain having to swap out the textboxes on the forms I've already created.

Initially I thought it would be great to use an extension method to do it. Basically creating an extension property for the textbox class and bind to that. From my limited extension method experience and doing a bit of checking online it looks like you can't do an extension property. As far as I can tell, binding has to be through a property since it needs to be able to get or set the value so an extension method wouldn't work.

I'd love to find a clean way to retrofit these forms using something like extension methods but if I have to create new textbox and combo box controls that's what I'll do.

My project is currently limited to .Net 2.0 due to the requirement to run on Windows 2000.

Any suggestions?

A: 

Ooh, nasty... I can only think of a couple of ways to achieve this and neither of them are what I would call ideal.

  • The first is to write a wrapper for your data object that contains nullables that converts all the nullables to empty strings. Bind to your wrapper object.

  • The second is to make sure that all the values in your database are not null... again, not ideal

  • The third you've already decided isn't viable in this situation is that you create a custom object that extends the textbox to add a property that can bind to a nullable.

  • The fourth most ideal way I could think of doesn't appear to be possible. Create an extension property which would allow you to extend all textbox objects and bind to that - but it doesn't appear that extension properties are possible at this time. It does seem like this is a situation where this type of feature would be particularly useful in the .NET framework.

BenAlabaster
+3  A: 

I've just stumbled across this problem myself and it's a real headache.

The funny thing about binding nullable types is that a DataGridView handles them without any problems - it's just the text boxes that cause problems.

It's pretty nasty - it even seems to prevent closing a form when you have an empty value in the text box and it appears that you can't tab away from it either.

So, it's not a great answer but my suggestion is to try to stick to datagridviews for the nullable types on a form.

Another suggestion is to use an 'extender provider' as suggested here though I haven't tested it yet:

EDIT: Have now downloaded the sample code on this page and it works BRILLIANTLY.

http://www.thejoyofcode.com/Databinding_and_Nullable_types_in_WinForms.NET.aspx

Damien
+1  A: 

In the comments section of the article referenced above one of the posters comes up with a simple solution.

Instead of binding with:

textBox1.DataBindings.Add("Text", myClass, "MyTextProperty");

Bind with:

textBox1.DataBindings.Add("Text", myClass, "MyTextProperty", true, DataSourceUpdateMode.OnPropertyChanged, string.Empty); 
shindigo