How to insert ComboBox item into ListBox? [winforms]
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
- Cast the
object
return value ofComboBox.SelectedItem
toDataRowView
, and add the value of the column you want to display. (i.e.listbox.Items.Add(((DataRowView)combobox.SelectedItem).FieldName);
- 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);
Ante
Updated on September 23, 2020Comments
-
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 over 14 yearsIt should work, if you check&exclude null first.
-
Ante over 14 yearsIt works but displays string "Systm.Data.DataRowView"
-
Henk Holterman over 14 yearsWhich would have been very valuable info to include in your question :-{
-
-
Ante over 14 yearstnx for help, second one solved the problem but slugster mentioned that first.. so i have to give him "nike sign"