简体   繁体   中英

save default value in combobox when nothing is selected c#

In my winform data entry application, I have a combo box set to "Dropdown List" so the user couldn't add any values other than items present in my combo box. but while saving if the user doesn't select any item in the combobox I need to save the form with default value("Mg"), but whenever I try to save it shows the field can't be null as it doesn't accept null values:

what I've tried during (save button click event) 1:

string unitcb = unitComboBox.Text;
 finishUOMComboBox.Text = uomtxt;

try2:

finishUOMComboBox.SelectedText = "Mg";

try3:

finishUOMComboBox.ValueMember = "Mg";

try4:

finishUOMComboBox.Text = "Mg";

try5:

finishUOMComboBox.selctedindex = 0;

please help how to save "mg" in the combbox when nothing is selected or it is null while saving.

Try this way to get ComboBox selected item:

finishUOMComboBox.selectedItem.ToString();

This code returns your selection's value

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM