C# 如何将 ComboBox 项插入到 ListBox 中?[winforms]
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1725991/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to insert ComboBox item into ListBox? [winforms]
提问by Ante
The question is very simple, How to insert ComboBox selected item into ListBox using c#?
问题很简单,如何使用c#将ComboBox选中项插入ListBox?
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..
和其他一些排列,但它总是显示 System.Data.DataRowView 或类似的东西..
EDIT: roblem was coused by this 2
编辑: roblem 被这个 2
lbList.DisplayMember = "hm";
lbList.ValueMember = "ID";
采纳答案by slugster
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.
您需要定义“不起作用”。什么地方出了错?
这个工作示例工作正常。要使用该对象(取消注释行)DisplayMember
,请确保设置该属性,请注意,我不必强制转换,因为我使用了该属性。
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; }
}
回答by ajrawson
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:
安特我认为问题来自comboBox.SelectedItem,它返回一个对象,在你的情况下,对象恰好是一个System.Data.DataRowView。我认为您需要将 combobox.selectedItem 转换为一个值。我是一个 VB 人,所以不确定 C# 的语法,但在 VB 中我们会做这样的事情:
DirectCast(combobox.SelectedItem, DataRowView).Foo
with foo being what ever value you want to pass to the listbox.
foo 是您想要传递给列表框的任何值。
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.
这返回和 Object 但它实际上是在组合框中显示的对象,天气它是一个字符串,int 等。不确定这是否有帮助,但我过去不得不做与此非常相似的事情,这就是解决方案我想出了。
回答by Neil Barnwell
The selected item of the combobox is a DataRowView, and the listbox is calling DataRowView.ToString()
to work out what to display.
组合框的选定项是 DataRowView,列表框正在调用DataRowView.ToString()
以计算要显示的内容。
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".
- 将
object
返回值转换为ComboBox.SelectedItem
toDataRowView
,并添加要显示的列的值。(IElistbox.Items.Add(((DataRowView)combobox.SelectedItem).FieldName);
- 设置列表框的“DisplayMember”和“ValueMember”值,这样列表框就不再使用
ToString()
了。这可能是您已经为您的 comboxbox 所做的事情,否则它也会显示“System.Data.DataRowView”。
回答by Henk Holterman
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);