Binding the combobox in C# desktop application
I am working on a project for my college where I need to bind data from database into the combobox. I need to store the roll no / enrollment no in the "value" field of combobox and na开发者_如何学运维me of the student in the "text" property of the combobox. How can I do that?>???
Please reply ASAP....
You will need to set the DataSource
of the combobox to your datasource. Then the ValueMember
for the Roll No and the DisplayMember
for the name of the student.
e.g
cboStudents.DataSource = dataSet1.Tables["Students"];
cboStudents.ValueMember = "RollNumber";
cboStudents.DisplayMember = "StudentName";
The two complex-bound controls you've most likely encountered are the ComboBox and the Listbox. To complex-bind one of these controls, you need to set the DataSource (where values originate), the DisplayMember (the name of the column of data that supplies the visible list items), and the ValueMember (the name of the column of data that supplies the possible control values).
combobox.DataSource = dataTable
combobox.ValueMember = "id"
combobox.DisplayMember = "name"
精彩评论