save default value in combox when nothing is selected

ravi kumar 331 Reputation points
2021-01-22T06:55:54.117+00:00

Hello all ,

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 selct 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";

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

Developer technologies Windows Forms
Developer technologies Visual Studio Other
Developer technologies C#
{count} votes

Accepted answer
  1. Abdulhakim M. Elrhumi 356 Reputation points
    2021-01-22T20:45:04.18+00:00

    Hi

    private void frmTestCombo_Load(object sender, EventArgs e)
        {
          Load_gender();
        }
    
    private void Load_gender()
        {
          List<string> gender = new List<string>();
          gender.Add("Male");
          gender.Add("Female");
          gender.ForEach(c =>
          {
            cmb_Gender.Properties.Items.Add(c);
          });
        }
    

    Best Regards.
    Please remember to mark the replies as answers if they help.

    1 person found this answer helpful.

2 additional answers

Sort by: Most helpful
  1. Alberto Poblacion 1,571 Reputation points
    2021-01-22T07:03:41.853+00:00

    You can try selecting by default the desired item in the combobox. This can be done by just setting the selected index:

    finishUOMComboBox.SelectedIndex = 0;

    Instead of 0, write the index of your default element, if it is not the first one.

    1 person found this answer helpful.

  2. ravi kumar 331 Reputation points
    2021-01-22T08:41:59.737+00:00

    hi @Viorel since i can't post on reply ..i am posting here:
    through this event:

    private void btnsave_Click(object sender, EventArgs e)  
            {  
                try  
                {  
                    String msg = "Confirm Save?";  
                    String caption = "Save Record";  
                    MessageBoxButtons buttons = MessageBoxButtons.YesNo;  
                    MessageBoxIcon ico = MessageBoxIcon.Question;  
                    DialogResult result;  
                    result = MessageBox.Show(this, msg, caption, buttons, ico);  
                    if (result == DialogResult.Yes)  
                    {  
                        generateautoID();  
      
                        string valueTextBox1 = materiaNumberTextBox.Text;  
       
                        string unitcb = unitComboBox.Text;  
          
      
                        this.iP_SpoolsBindingSource.EndEdit();  
      
                        MessageBox.Show("The Record saved Successfully:" + outputSpoolNoTextBox.Text, "Save_Update",  
                            MessageBoxButtons.OK, MessageBoxIcon.Information);  
      
                        this.iP_SpoolsTableAdapter.Update(this.pINQCDataSet.IP_Spools);  
                        this.iP_SpoolsTableAdapter.Fill(this.pINQCDataSet.IP_Spools);  
      
                        this.iP_SpoolsBindingSource.AddNew();  
      
                        string strStartenddateformat = "dd-MM-yyyy";  
      
    ;  
                        unitComboBox.SelectedIndex = 1;  
      
                          
                        materiaNumberTextBox.Text = valueTextBox1;  
      
      
                        dOEDateTimePicker.Format = DateTimePickerFormat.Custom;  
                        dOEDateTimePicker.CustomFormat = strStartenddateformat;  
                        dOEDateTimePicker.Value = DateTime.Now;  
      
                        dOPDateTimePicker.Format = DateTimePickerFormat.Custom;  
                        dOPDateTimePicker.CustomFormat = strStartenddateformat;  
                        dOPDateTimePicker.Value = DateTime.Now;  
                    }  
                    else  
                    {  
                        return;  
                    }  
                }  
                catch (Exception ex)  
                {  
                    MessageBox.Show("Saving Failed:" + ex.Message.ToString(), "Save",  
                        MessageBoxButtons.OK, MessageBoxIcon.Error);  
                }  
            }  
    

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.