How to use the selectedITem or SelectedValue when selecting an item from a combobox?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Frans vd Heuvel
    New Member
    • Jan 2011
    • 2

    #1

    How to use the selectedITem or SelectedValue when selecting an item from a combobox?

    I have the following problem

    How do you select an item from a combobox when this combobox receives data from a SQL database.
    Code:
    private void cmbMDCTestRequirements_SelectedValueChanged(object sender, EventArgs e)
    
    Object selectedRequirement = cmbMDCTestRequirements.selectedItem;
    
    Now this Object should be changed into an integer.
    
    How should this be done ?
    The cmbMDCTestRequi rements firs tis filled with data uysing the following code:

    Code:
            private void FillDataInApplicationRequirementComboBox()
            {
                cmbMDCTestRequirements.DataSource = null;
                ApplicationSettings appSettings = new ApplicationSettings();
                dataset = appSettings.getApplicationRequirements();
                dsView = dataset.DefaultViewManager;
                cmbMDCTestRequirements.ValueMember = "ApplicationRequirements.Id";
                cmbMDCTestRequirements.DisplayMember = "ApplicationRequirements.RequirementDescription";
                cmbMDCTestRequirements.DataSource = dsView;
    
                totalCustomerRequirements = cmbMDCTestRequirements.Items.Count;
                txtTotalCustomerRequirements.Text = totalCustomerRequirements.ToString();
            }
  • GaryTexmo
    Recognized Expert Top Contributor
    • Jul 2009
    • 1501

    #2
    Can you use SelectedIndex, perhaps?

    Comment

    • Frans vd Heuvel
      New Member
      • Jan 2011
      • 2

      #3
      Hi Gary, thanks for you answer, but no i don't like to use the SelectedIndex, because it gives the position in the combobox and not in the table of the database. but in the meantime i already have found the answer.

      The answer is using the

      Code:
      DataRowView selectedRequirementItem = cmbMDCTestRequirements.SelectedItem as DataRowView;
      if (selectedRequirementItem != null)
        {
           SelectedValue = selectedRequirementItem.Row.ItemArray[0].ToString();
        }
      Now you really select the Id of a record of the database.

      Thanks for your quick answer

      Comment

      Working...