Click here to Skip to main content
13,097,012 members (78,859 online)
Rate this:
 
Please Sign up or sign in to vote.
See more:
I have a table in sqlServer like this:

ID Title Description
-----------------------------------
1 Director
3 Actor
5 Producer

And in my project there is 2 Form:
1. Activity
2. ActivityDialog

When I Click on Edit_Click() the ActivityDialog will be shown.

There is a DataBound comboBox in ActivityDialog Form which its DisplayMember is "Title" and ValueMember is "ID".
At ActivityDialog_Load() I want to set selectedIndex for comboBox to show the current record's ActivityType.

I have tried these but both of them returns -1:

comboBox.SelectedIndex = comboBox.Items.IndexOf(1);

And
comboBox.SelectedIndex = comboBox.Items.IndexOf("Director");
Posted 16-Aug-12 11:45am
Updated 16-Aug-12 12:00pm
v2
Comments
Sergey Alexandrovich Kryukov 16-Aug-12 19:39pm
   
To be clear, you need to show the code sample where you add elements to the control.
--SA

1 solution

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

Try this:
comboBox.SelectedIndex = 1;

and the combobox is usually boiunded to a collection, so get the index in the collection in code insted.
  Permalink  
v2
Comments
Meysam Tolouee 16-Aug-12 17:52pm
   
You didn't understand.
The form is an Edit Dialog so I should Set the comboBox value dynamically.
Kenneth Haugland 16-Aug-12 17:55pm
   
Now I really dont understand you rproblem. If you wan tto select a specific index you should get it by the datatable or list you are populationg the items from. If you have recently added the item and want the last you added, simply use comboBox.Items.Count-1.
Meysam Tolouee 16-Aug-12 18:01pm
   
See new Version of question.
Kenneth Haugland 16-Aug-12 18:04pm
   
It was just more confusing, if you whish to get the element you simply call the selectedindex and do something like MyList(comboBox.SelectedIndex) to get the corresponding row...
Meysam Tolouee 16-Aug-12 18:14pm
   
When comboBox is not DataBound IndexOf() work perfectly.
for example I'd tried this before:

int year = 2012;
for (int i = 0; i < 20; i++)
{
this.comboBox.Items.Add(year);
year--;
}
then somewhere else:
comboBox.SelectedIndex = comboBox.Items.IndexOf("2010");
And the SelectedIndex value got 2;
Kenneth Haugland 16-Aug-12 18:18pm
   
Have you put a debug mark in the solution to check that the item actually exists, no spelling mistakes or anythong like that?
Meysam Tolouee 16-Aug-12 18:25pm
   
Yes, Many times.
And I figure it out finally.
Next time test the codes first then make any suggestion and if you miss sth then ask.
Thank you anyway for your time.
Sergey Alexandrovich Kryukov 16-Aug-12 19:40pm
   
Next time please think at your question to make sure you provide sufficient information.
--SA
Sergey Alexandrovich Kryukov 16-Aug-12 19:41pm
   
My 5. It's hard to keep patience...
--SA
Kenneth Haugland 16-Aug-12 20:11pm
   
Thank you, Sergey :)
ridoy 17-Aug-12 2:04am
   
comboBox.selectedIndex =1 is the right solution

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy |
Web03 | 2.8.170813.1 | Last Updated 16 Aug 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100