English 中文(简体)
How to insert ComboBox item into ListBox? [winforms]
原标题:

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";
最佳回答

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; }
}
问题回答

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

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

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.





相关问题
Bring window to foreground after Mutex fails

I was wondering if someone can tell me what would be the best way to bring my application to the foreground if a mutex was not able to be created for a new instance. E.g.: Application X is running ...

How to start WinForm app minimized to tray?

I ve successfully created an app that minimizes to the tray using a NotifyIcon. When the form is manually closed it is successfully hidden from the desktop, taskbar, and alt-tab. The problem occurs ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

Handle DataTable.DataRow cell change event

I have a DataTable that has several DataColumns and DataRow. Now i would like to handle an event when cell of this DataRow is changed. How to do this in c#?

Apparent Memory Leak in DataGridView

How do you force a DataGridView to release its reference to a bound DataSet? We have a rather large dataset being displayed in a DataGridView and noticed that resources were not being freed after the ...

ALT Key Shortcuts Hidden

I am using VS2008 and creating forms. By default, the underscore of the character in a textbox when using an ampersand is not shown when I run the application. ex. "&Goto Here" is not ...

WPF-XAML window in Winforms Application

I have a Winforms application coded in VS C# 2008 and want to insert a WPF window into the window pane of Winforms application. Could you explain me how this is done.

热门标签