Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > TextBox field - binding to a NULL value and empty string in the field
 

TextBox field - binding to a NULL value and empty string in the field

Hi

I have a TextBox bound to a NULLable datacolumn in a DataTable.

When the form is displayed and I enter a value in the entry field and try to tab out, it wouldn't go to the next control.
Basically, the validation fails and the user is stuck in that field under a value is entered in that field.

So, I ended up have the following code:

...
...
...
textBox.DataBindings["Text"].BindingComplete += delegate(object sender1, BindingCompleteEventArgs evt)
{
if (evt.BindingCompleteState == BindingCompleteState.Success)
{
if (evt.Binding.Control.Text == string.Empty)
{
dataRow.SetValueNull();
}
}
};
textBox.DataBindings["Text"].Parse += new ConvertEventHandler(EmptyValue_Parse);


void EmptyValue_Parse(object sender, ConvertEventArgs e)
{
if (e.Value != null && e.Value.ToString() == string.Empty)
{
e.Value = null;
}
}
...
...
...

I tried the binding to the TextBox as follows:

...
...
...
this.textBox.DataBindings.Add("Text", dataRow, "rest_bp_diastolic", true, DataSourceUpdateMode.OnValidation, string.Empty);
...
...
...

The last parameter "string.Empty" is supposed to be used as the value to used
when a DBNull is returned from the data source. Shouldn't the other way also work - if
the value in the text box is string.Empty as I specified, shouldn't the data source be updated with DbNull?

Anyone know what I am missing and if there is a simpler way avoiding the above handlers?
athadu  Friday, December 29, 2006 7:54 PM

Wow ... I don't know why you had to jump through all those hoops ... just a simple databind should have worked. I just use something like:

this.textBox.DataBindings.Add("Text", dataRow, "rest_bp_diastolic");

Do you have a Validate event handler that's preventing a DBNull.Value in this column?

[UPDATE] Ooops, let me change that just a bit:

this.textBox.DataBindings.Add("Text", MyDataTable, "rest_bp_diastolic");

BonnieB  Sunday, December 31, 2006 1:55 AM

Wow ... I don't know why you had to jump through all those hoops ... just a simple databind should have worked. I just use something like:

this.textBox.DataBindings.Add("Text", dataRow, "rest_bp_diastolic");

Do you have a Validate event handler that's preventing a DBNull.Value in this column?

[UPDATE] Ooops, let me change that just a bit:

this.textBox.DataBindings.Add("Text", MyDataTable, "rest_bp_diastolic");

BonnieB  Sunday, December 31, 2006 1:55 AM

You can use google to search for other answers

Custom Search

More Threads

• DataGridView with Unbound Columns
• Loop through datagrid to get values of boolean columns
• datagridview columns databindings
• dataGridview view record
• The Old Datagrid Button Question...
• DataGridView: Is data binding really a good solution to bind a List of objects to a DGV?
• DataGridView row height
• Parent/Child-Save options
• DataBinding Problem?
• How to make .NET DataGridView columns collapsible on header click