简体   繁体   中英

Get Data and Map to Controls from Combobox Selected Value C#

I am trying to get required data into controls when i select specific Student Name from a combobox. i am using SelectedValueChanged event for this and the valuemember for that combobox in order to populate it with required data.

StudentsNamesComboBox.DisplayMember = "StudentName";
StudentsNamesComboBox.ValueMember = "SectionId";

here the studentnameCombobox has a different value from the parameter value required to get specific record from database using storedprocedure.

cmd.Parameters.AddWithValue("@AdmissionNumber", Convert.ToInt16(StudentsNamesComboBox.SelectedValue));

the problem i am facing is that i can't get the required parameter value (@AdmissionNumber) from the selected name in the studentNamesCombox. any idea how to get the required parameter value without changing the studentNamesCombox valuemember? any other way around? because changing vlauemember will affect the whole functionality. Regards

When you are populating StudentName combobox, why don't you set the ValueMember of that combobox to "AdmissionNumber".

You do not need SectionI of the student in that combobox. SectionId is already available in the other combobox. In StudentName combobox all the items have same sectionId. Correct me if my assumption is wrong.

This way ComboBox.SelectedValue will give the value associated with the selected item which in your case will be "AdmissionNumber".

To get the name of the selected student you need to use ComboBox.SelectedItem.Text. That will give the Selected StudentName in your case.

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