Data Binding Text Box Cannot Exit

I have the same problem as this post . It is called bij by a numeric NULL value for my dataset object. When the property of this object has an initial value of null, I can exit my text box. When my text field has an initial numeric value and clears the text field, I cannot exit.

I want to be able to specify a null value by clearing the text field. I know this is an important issue, when the CausesValidating property is false, I can exit. Also my property setting function is never reached.

Any ideas or suggestions? Thank you very much in advance!

+4
source share
1 answer

(Sorry, at first I did not see your answer asking for an easier way than manually connecting to Format / Parse events, so my answer is probably not sufficient, but for other people having the same problem, a code snippet can be useful. )

You can use the Format and Parse event bindings of this TextBox to convert the empty string to DBNull.Value and vice versa so that the control is right, and you can leave it.

// Call AllowEmptyValueForTextbox() for each TextBox during initialization. void AllowEmptyValueForTextBox(TextBox textBox) { if (textBox.DataBindings["Text"] != null) { textBox.DataBindings["Text"].Format += OnTextBoxBindingFormat; textBox.DataBindings["Text"].Parse += OnTextBoxBindingParse; } } void OnTextBoxBindingParse(object sender, ConvertEventArgs e) { // Convert the value from the textbox to a value in the dataset. string value = Convert.ToString(e.Value); if (String.IsNullOrEmpty(value)) { e.Value = DBNull.Value; } } void OnTextBoxBindingFormat(object sender, ConvertEventArgs e) { // Convert the value from the dataset to a value in the textbox. if (e.Value == DBNull.Value) { e.Value = String.Empty; } } 

Instead of showing the user an empty text field when the field in the dataset is empty, you can use the same mechanism to populate the text field with a string like "(not set)" orso. In the Format method, convert DBNull.Value to "(not set)" and in the Parse method, convert it back.

See also: http://msdn.microsoft.com/en-us/library/system.windows.forms.binding.parse

+4
source

Source: https://habr.com/ru/post/1334685/


All Articles