Quantcast
Channel: Windows Forms Data Controls and Databinding forum
Viewing all articles
Browse latest Browse all 2535

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

$
0
0
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?

Viewing all articles
Browse latest Browse all 2535

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>