How can I fill a combobox from sql database ( students table with id, and name columns ) , the display text represents the name of a student and the value of the item of combobox is the id for that student that when I get the value of the combobox I will get the id value
相关问题
- Sorting 3 numbers without branching [closed]
- Graphics.DrawImage() - Throws out of memory except
- Generic Generics in Managed C++
- Why am I getting UnauthorizedAccessException on th
- 求获取指定qq 资料的方法
Below are the important properties for you.
ComboBox.DataSource Property
ComboBox.DisplayMember Property
ComboBox.ValueMember Property
Here is one way if you want to add items programmatically.
There are various ways of doing this.
How to: Add and Remove Items from a Windows Forms ComboBox
ComboBox.Items Property
First off you need to figure out how you're going to get the data back from the DB, but I'll assume you either know that or intend to ask another question in regards to that. From there, your best bet is to bind some collection to the
ComboBox
. Here is an example of doing that with aDataSet
. You can also bind toList<T>
or otherIEnumerable<T>
, which would make more sense if you're going to use LINQ to get at the data. Here is a question here on SO about binding aList
to aComboBox
Perhaps you could tell us how you intend to get at the data so we could give you a more tailored answer?