Click here to Skip to main content
15,867,568 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi, i have Table in my database .my table have 2 Columns: ID and Name
i want to my combobox items fill by (Name Column values)
and combobox Index start by (id columns. )
for example in table
__________________
ID | Name |
------------------
1 | john |
3 | mary |
5 | nick |
------------------
I want to combobox first item = john and it index =1 ) and other rows like this
I can fill combobox items by this code:

C#
datatable dt;
int arr[]=new int [dt.rows.count];
for (int i=0; i < dt.rows.count ;i++)
{
   combobox.items.add(dt.rows[0]["name"]);
}

---------------------------------------------------
But I can't equal id to index of combobox items
help me
Posted
Updated 12-Jun-14 9:43am
v2

Index will always start from 0. You can do one thing. Just add one default item (like "--Select--") in ComboBox before the loop, so that it will be inserted at index 0.

Then the loop will automatically start inserting from index 1. But you cannot match the index exactly as you have written. That is because one index cannot be left blank.

However, you can store that ID as the ComboBox Item Value.
 
Share this answer
 
v2
Comments
Kyle Moyer 12-Jun-14 14:29pm    
Judging by the example, the IDs are not guaranteed to be sequential. This will cause the first list item (john) to have an index matching the ID, but not the rest.
Look, I just gave the solution to add another extra item at the first index that is 0 so that second Item starts with 1. There will be no way to match the index exactly as it is given in the example by OP. That is because some index are also missing like index 2 and 4. As you suggested he should put that in the property value so that he can get that afterwords.
I have updated my answer. Thanks :)
The items in a combo-box are like items in any other collection; they have an auto-incrementing zero-based index. In order to accomplish what you are asking, you would need to insert 'dummy' entries for the indexes that you do not have in the table. In your example, this would for indexes 0, 2, and 4.

What you really want to do, is add Key-Value pair objects into your item collection. There are varying ways to do this depending on whether you're using Forms, WPF, or Web controls. Since you use the term 'ComboBox' I'm going to assume you're using Forms. In this case the simplest thing to do is add KeyValuePair objects to your item collection as follows:

C#
comboBox.DisplayMember = "Value";
foreach (DataRow row in dt.Rows)
{
    comboBox.Items.Add(
        new KeyValuePair<int, string>(
            Convert.ToInt32(row[0]),
            Convert.ToString(row[1])));
}


The first line sets the member you want displayed as text in your combo-box. By default the toString method is called, which will leave you with something like "[1, john]". Setting DisplayMember to "Value" will cause only "john" to be displayed. You can then access the values of the selected item as follows:

C#
if (comboBox.SelectedItem is KeyValuePair<int, string>)
{
    int selectedKey = ((KeyValuePair<int, string>)comboBox.SelectedItem).Key; //1
    string selectedValue = ((KeyValuePair<int, string>)comboBox.SelectedItem).Value; //john
}
else
{
    //Nothing selected
}
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900