Click here to Skip to main content
15,888,600 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I want to select a particular item from combobox containing 'n' items, so that i can display that item on combobox while form is running.
note: i have stored the 'n' items in an array and then added them to combobox using datasource
like :
comboBox1.DataSource = array1;


code sample:
 string[] array2 = new string[3] { "10:00", "16:00", "22:00" };
            string[] array1 = new string[3] { "8:30", "13:30", "19:00" };
            if (no == 1 || no == 3)
            {
                comboBox1.DataSource = array1;
            }
            if (no == 2 || no == 4)
            {
                comboBox1.DataSource = array2;
            }
            if (visit == 1 && j != -1)//here is the problem it
            {
                comboBox1.SelectedIndex = j;
            }
//j has a value say 1, so it should display 13:30 for 'no'=1 or 3
but it dosent


What I have tried:

i have tried selectedindex, selectedvalue, selectedtext, selecteditem but nothing works it always displays index 0 item (i.e first item).
Also it dosent gives an error but dosent change the displayed item in combobox its always the first item
Posted
Updated 13-Jun-17 1:27am
v2
Comments
Richard MacCutchan 13-Jun-17 6:20am    
Nothing works? What does that mean? Please show your code so people can help you, we cannot guess what you are doing.
[no name] 13-Jun-17 6:40am    
And while we are at it, 'C#' does not really tell us what control we are talking about. WinForms? WPF? Silverlight? ASP .Net?
Richard MacCutchan 13-Jun-17 6:43am    
I don't think you meant to ask me that question.
[no name] 13-Jun-17 7:19am    
Not important anyway. As I see, quite a few psycics already have written answers. :-)

And I got to get used to looking for the invisible 'reply' button instead of using the first thing I see :-)
chinu1d 13-Jun-17 7:26am    
string[] array2 = new string[3] { "10:00", "16:00", "22:00" };
string[] array1 = new string[3] { "8:30", "13:30", "19:00" };
if (no == 1 || no == 3)
{
comboBox1.DataSource = array1;
}
if (no == 2 || no == 4)
{
comboBox1.DataSource = array2;
}
if (visit == 1 && j != -1)//here is the problem it
{
comboBox1.SelectedIndex = j;
}
//j has a value say 1, so it should display 13:30 for 'no'=1 or 3
but it dosent

if the items in your ComboBox are strings, you can try:
comboBox1.SelectedItem = "test1";

If the above is not working then try with following method
 
public static void SelectItemByValue(this ComboBox cbo, string value)
{
    for(int i=0; i < cbo.Items.Count; i++)
    {
        var prop = cbo.Items[i].GetType().GetProperty(cbo.ValueMember);
        if (prop!=null && prop.GetValue(cbo.Items[i], null).ToString() == value)
        {
             cbo.SelectedIndex = i;
             break;
        }
    } 
}
Then just consume the method:

ddl.SelectItemByValue(value);
 
Share this answer
 
Maybe you need something like:
C#
comboBox1.DataBindings.Add(new System.Windows.Forms.Binding("SelectedValue", bsCustomers, "id", true));
See this question: c# - Set SelectedItem on a combobox bound to datasource - Stack Overflow[^]
Also there might be a problem with some property you changed in the Forms designer, just try your code with a fresh Form and a ComboBox with default properties.
 
Share this answer
 
v2
Works for me:
public class MyObject
    {
    public string Text { get; set; }
    public int Value { get; set; }
    }
private void MyButton_Click(object sender, EventArgs e)
    {
    MyObject[] data = new MyObject[] {new MyObject(){Text = "abc", Value = 1},
                                      new MyObject(){Text = "def", Value = 2},
                                      new MyObject(){Text = "ghi", Value = 3}};
    myComboBox.DataSource = data;
    myComboBox.ValueMember = "Value";
    myComboBox.SelectedValue = 2;

Or
myComboBox.SelectedItem  = data[1];

Or
myCOmboBox.SelectedText  = "abc";

So what am I doing that is different to you?
 
Share this answer
 
Comments
[no name] 13-Jun-17 6:43am    
-- So what am I doing that is different to you?

He uses ASP .Net MVP. Just joking, but how do you all know what UI he is actually talking about?
OriginalGriff 13-Jun-17 7:18am    
I don't. But it's a starting point for him to understand that it does work somewhere... :laugh:

My personal guess is that he has an array of strings, which won't work as a data source (since they only have a Length property) - I could be wrong though.
chinu1d 13-Jun-17 7:29am    
But i have tried it it dosent seems to work,
maybe i will try again
OriginalGriff 13-Jun-17 7:36am    
What are we doing that is different?
chinu1d 13-Jun-17 7:41am    
see my code

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