How to insert ComboBox item into ListBox? [winforms]

23,100

Solution 1

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; }
}

Solution 2

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.

Solution 3

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".

Solution 4

You can easily get at the (untyped) row:

DataRowView drv = (DataRowView) combobox.SelectedItem;
DataRow row = drv.Row;

After that it depends on what colum you need, if you know the Column name:

object value = row["Column"];
listbox.Items.Add(value);     
Share:
23,100
Ante
Author by

Ante

Updated on September 23, 2020

Comments

  • Ante
    Ante over 3 years

    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";
    
    • Henk Holterman
      Henk Holterman over 14 years
      It should work, if you check&exclude null first.
    • Ante
      Ante over 14 years
      It works but displays string "Systm.Data.DataRowView"
    • Henk Holterman
      Henk Holterman over 14 years
      Which would have been very valuable info to include in your question :-{
  • Ante
    Ante over 14 years
    tnx for help, second one solved the problem but slugster mentioned that first.. so i have to give him "nike sign"