views:

1346

answers:

3

The question is very simple, How to insert ComboBox selected item into ListBox using c#?

I have tried with this:

listbox.Items.Add(combobox.SelectedItem);

and some other permutations but it always displays System.Data.DataRowView or something like that..

EDIT: roblem was coused by this 2

lbList.DisplayMember = "hm";
lbList.ValueMember = "ID";
+1  A: 

Ante I think the issue comes from comboBox.SelectedItem, this returns an Object and in your case that Object happens to be a System.Data.DataRowView. I think you'll need to cast the combobox.selectedItem to a value. I'm a VB guy so not sure the syntax for C# but in VB we'd do something like this:

DirectCast(combobox.SelectedItem, DataRowView).Foo

with foo being what ever value you want to pass to the listbox.

Another option that might work, if your intention is to include the value of the combo box in the listbox is to use:

combobox.selectedvalue

this returns and Object but it's actually the object that is being displayed in the combobox weather it be a string, int, etc. Not sure if that helps but I've had to do something very similar to this in the past and that's the solution I came up with.

ajrawson
+1  A: 

The selected item of the combobox is a DataRowView, and the listbox is calling DataRowView.ToString() to work out what to display.

You can either

  1. Cast the object return value of ComboBox.SelectedItem to DataRowView, and add the value of the column you want to display. (i.e. listbox.Items.Add(((DataRowView)combobox.SelectedItem).FieldName);
  2. Set the "DisplayMember" and "ValueMember" values of the listbox, so the listbox doesn't just use ToString() any more. This is probably something you've already done for your comboxbox, otherwise it would also be displaying "System.Data.DataRowView".
Neil Barnwell
tnx for help, second one solved the problem but slugster mentioned that first.. so i have to give him "nike sign"
Ante B.
+2  A: 

You need to define "doesn't work". What went wrong? This works example works fine. To use the object (uncomment the lines) make sure you set the DisplayMember property, note that i am not having to cast because i use that property.

public partial class Form1 : Form
{
    private void Form1_Load(object sender, EventArgs e)
    {


        List<string> x = new List<string>();
        x.Add("A");
        x.Add("B");
        x.Add("C");
        x.Add("D");
        x.Add("B");


        List<Client> z = new List<Client>();
        z.Add(new Client() { Name = "A" });
        z.Add(new Client() { Name = "B" });
        z.Add(new Client() { Name = "C" });

        comboBox.Items.AddRange(x.ToArray());

        //comboBox.DisplayMember = "Name";
        //listBox.DisplayMember = "Name";
        //comboBox.Items.AddRange(z.ToArray());


    }

    private void comboBox_SelectedIndexChanged(object sender, EventArgs e)
    {
        listBox.Items.Add(comboBox.SelectedItem);
    }
}


public class Client
{
    public string Name { get; set; }
}
slugster