Click here to Skip to main content
15,879,535 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:

I have one string which holds value and i want to search that value from combobox and assign as selected item.

i am using following code but not there any other way?

cmbCameraName contains items: Add,Delete,Modify

string CameraName="Add";
int index =Convert.ToInt16(cmbCameraName.FindName(CameraName));
                      cmbCameraName.SelectedIndex = index;

Updated 24-Nov-11 18:54pm
rajeevcapgeminiindia 25-Nov-11 1:37am    
In comboBox you can store two things
-Display Text
-Combo Value

If you select display Text, then write following code to get Combo Value
Sergey Alexandrovich Kryukov 25-Nov-11 2:01am    
Did you try to see the value of index under debugger. -1? 0?

1 solution

are you sure that your combobox has a value?

if it has a value then try this:

int index =Convert.ToInt16(cmbCameraName.FindName(CameraName));
                      cmbCameraName.SelectedIndex = index;

if ur still encountering a probelm ask me,,,

thanks hope it helps. :)
Share this answer
KIDYA 25-Nov-11 1:38am    
Still encountering a problem.I am loading values into Combobox before calling this code and it contains values.
D K N T H 25-Nov-11 1:51am    
try this:

int index =Convert.ToInt16(combobox.selectedvalue = index);
KIDYA 25-Nov-11 2:07am    
Initially index is on -1.No any value is selected.above code will work for it?
D K N T H 25-Nov-11 2:18am    
try this:

string CameraName="Add";
int index =Convert.ToInt16(cmbCameraName.SelectedValue = CameraName));

mark this as an answer if it helps you..

KIDYA 25-Nov-11 5:44am    
Not an error input string is not in correct format

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