Click here to Skip to main content
15,896,434 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi I am
binding combo box Using following code
combobox binding code
C#
cProduct obj = new cProduct();
            obj.ppro_CompanyId = csession.CompanyId;
            obj.ppro_IsActive = "true";
            cmbProduct.DisplayMember = "ppro_ProductName";
            cmbProduct.ValueMember ="ppro_ProductId";
            cmbProduct.DataSource = obj.GetProduct();//returns Array list

Now Want to programatically select the combobox to a specified ValueMember

Please help me
Posted
Updated 5-Mar-14 1:37am
v3

1 solution

You can set SelectedIndex property for the combobox after doing data binding.

Update: This just works.

XML
List<MyClass> lst = new List<MyClass>();
           lst.Add(new MyClass { MyProperty = 1, MyProperty1 = "1" });
           lst.Add(new MyClass { MyProperty = 2, MyProperty1 = "2" });
           lst.Add(new MyClass { MyProperty = 3, MyProperty1 = "3" });

           comboBox1.DataSource = lst;
           comboBox1.DisplayMember = "MyProperty1";
           comboBox1.ValueMember = "MyProperty";

           comboBox1.SelectedValue = 2;
 
Share this answer
 
v2
Comments
Member 10296413 6-Mar-14 0:44am    
Thank you for your responce
My problem is how to get the index(I have to search the index by value field
for the Same case in Asp Dropdown I'm using the following code
ddScreanNameClass.SelectedIndex = ddScreanNameClass.Items.IndexOf(ddScreanNameClass.Items.FindByValue("3"));

I have to do the Same for comboxes in Windows form application
dan!sh 6-Mar-14 0:48am    
You also try using SelectedValue property.
Member 10296413 6-Mar-14 0:59am    
if I use selected value property the value field of the selected Item will change and display field is not.

I want to pragmatically select the item in which the value field is specified value field
I done it in As using the code specified I first time using windows form application Please help me
dan!sh 6-Mar-14 1:12am    
Updated the reply.
Member 10296413 6-Mar-14 1:30am    
In ur code the display and value field are same
it wont work if you are use different like

List<myclass> lst = new List<myclass>();
lst.Add(new MyClass { MyProperty = 1, MyProperty1 = "First" });
lst.Add(new MyClass { MyProperty = 2, MyProperty1 = "Second" });
lst.Add(new MyClass { MyProperty = 3, MyProperty1 = "Third" });

comboBox1.DataSource = lst;
comboBox1.DisplayMember = "MyProperty1";
comboBox1.ValueMember = "MyProperty";

comboBox1.SelectedValue = 2;

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