Click here to Skip to main content
15,069,020 members
Please Sign up or sign in to vote.
4.33/5 (2 votes)
See more:
i want to retrieve Index of the items contained in a combobox.

for eg

comboBox_dept is populated with items from database as "one","two","three"


i need to find their indexes pro-grammatically.

i tried this

MIDL
int  index = comboBox_dept.FindString(one);
       comboBox_dept.SelectedIndex = index;

it always return -1.

C#
foreach (Object item in comboBox_dept.Items)
               {
                  int index1 = comboBox_dept.Items.IndexOf(item);
               MessageBox.Show(index1.ToString() + item);
               }


it only return 1st item only

any help or hint would be appreciated.
Posted
Updated 10-Jun-21 22:21pm
v2
Comments
Simon_Whale 4-Jul-11 17:45pm
   
how are you adding them to the combobox?
sanomama 4-Jul-11 17:50pm
   
using sqldatareader.
Christian Graus 4-Jul-11 21:19pm
   
A sqldatareader cannot add items to a combo box. You must have code that's reading the DB, if you're iterating over the items, then you know the index as you put them in, and until you put them in, they have no index, but you can tell what it will be, by counting the items in your data source, assuming it's sorted, or the combobox is not sorted.
sanomama 4-Jul-11 22:25pm
   
SqlConnection con1 = new SqlConnection(str);
SqlCommand cmd = new SqlCommand("SELECT * from department ", con1); SqlDataReader DR = cmd.ExecuteReader();
while (DR.Read() && DR.HasRows) { comboBox_dept.Items.Add(DR["name"].ToString()); //name is the table department column from database }
i have added i like this.

Why don't you do a loop:

for (int i = 0; i < comboBox_dept.Items.Count;++i)
{
string s = comboBox_dept.Items[i].ToString();
// Now s is the string and i is the index
}
   
Comments
sanomama 4-Jul-11 17:33pm
   
items in the combobox are from database. so while looping counts give result zero.
sanomama 4-Jul-11 17:39pm
   
the loops return indexes if we have hard coded the items in combobox. but i have added the items from database using datareader.
Christian Graus 4-Jul-11 21:18pm
   
If the items are in the combobox, then you can use a loop and it will work, how can it not ? If your items are not yet in a combo box, then they have no index.
sanomama 4-Jul-11 22:27pm
   
i hardcoded the items from windows form n used the loop. it works but when i populate the combobox from database n use the loop, counts say null. but the items are in combox box. can suggest what i might be doing wrong.
Christian Graus 4-Jul-11 23:11pm
   
You need to post your code. If the Item Count is 0, then there's no items in your combobox. You must be checking before the items are there, nothing else makes sense.
sanomama 5-Jul-11 2:51am
   
i solve it. thanks for the forloop code :)
It is not clear how items are added to the combobox? Are you doing something like comboBox_dept.Items.Add(sqldatareader);?

Add will add a single object to the list of items whatever the type of the object. If you want to add a range, then uses AddRange.

Your last code snippet seems to indicate that your list contains only one item. Are you seeing multiple items in the combo at run time.

If you are using databinding, I guess that should get the index from the original source.
   
Comments
sanomama 4-Jul-11 22:16pm
   
SqlConnection con1 = new SqlConnection(str);
SqlCommand cmd = new SqlCommand("SELECT * from department ", con1);
SqlDataReader DR = cmd.ExecuteReader();
while (DR.Read() && DR.HasRows)
{ comboBox_dept.Items.Add(DR["name"].ToString()); //name is the table department column from database
}
sanomama 4-Jul-11 22:19pm
   
i had addded item in above way. my combo box contains multiple items now. i need the check the items whether it exist in combobox ur not n gets its index too . can you provide some hints?
Christian Graus 4-Jul-11 23:11pm
   
I don't see any way in which, having done this, looping in the ways we've said, could fail. It has to work, if your combo box contains items. It does not care where they came from.
Philippe Mori 5-Jul-11 7:41am
   
By the way, if your database column are fixed width text, they are filled with spaces. Thus "abc" might be stored as "abc ". Check that the strings that are added does not contains extra spaces or trim them.
sanomama 6-Jul-11 21:05pm
   
thanks all for the replies.
Try this code:

VB
 Public Function GetTextDataGridView(ByVal DataGR As DataGridView, ByVal Rowget As Integer) As String

    GetTextDataGridView = vbNullString
    Try
        Dim i As Integer
        i = DataGR.CurrentRow.Index
        GetTextDataGridView = DataGR.Item(Rowget, i).Value
    Catch ex As Exception

    End Try

End Function


in Form_Load()

VB
Dim item As Object = GetTextDataGridView(frmListMembers.dgvListMember, 3)
Dim Index1 As Integer = cboSex.Items.IndexOf(item)
cboSex.SelectedIndex = Index1
   
int  index = comboBox_dept.FindString(one);


This returns the index of the item but you have to look for the text (DisplayMember, not ValueMember).

If you want to look for the ValueMember you can use

comboBox_dept.SelectedValue = "two";

int position = comboBox_dept.SelectedIndex; // This will return the position you are looking for
   
Comments
Richard MacCutchan 11-Jun-21 4:33am
   
Given that this was answered 10 years ago, it is unlikely that the questioner is still waiting for any more replies.
Mimí_BD 17-Jun-21 6:29am
   
Surely he won't... but for example i found the page looking for the same so I think it could be useful for another.

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