简体   繁体   English

从组合框选定值C#获取数据并映射到控件

[英]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. 我为此使用了SelectedValueChanged事件和该组合框的valuemember,以便用所需的数据填充它。

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. 这里的studentnameCombobox的值与使用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. 我面临的问题是我无法从studentNamesCombox中的所选名称中获取所需的参数值(@AdmissionNumber)。 any idea how to get the required parameter value without changing the studentNamesCombox valuemember? 任何想法如何获取所需的参数值而无需更改studentNamesCombox valuemember? any other way around? 还有其他办法吗? because changing vlauemember will affect the whole functionality. 因为更改vlauemember会影响整个功能。 Regards 问候

When you are populating StudentName combobox, why don't you set the ValueMember of that combobox to "AdmissionNumber". 当您填充StudentName组合框时,为什么不将该组合框的ValueMember设置为“ AdmissionNumber”。

You do not need SectionI of the student in that combobox. 您不需要该组合框中的学生的第一节。 SectionId is already available in the other combobox. SectionId在另一个组合框中已经可用。 In StudentName combobox all the items have same sectionId. 在StudentName组合框中,所有项目都具有相同的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". 这样ComboBox.SelectedValue将提供与所选项目关联的值,在您的情况下为“ AdmissionNumber”。

To get the name of the selected student you need to use ComboBox.SelectedItem.Text. 要获取所选学生的姓名,您需要使用ComboBox.SelectedItem.Text。 That will give the Selected StudentName in your case. 这将为您提供所选的StudentName。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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