Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/333.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# ListBox无法显示BindingList数据,而是显示对象名称_C#_Listbox - Fatal编程技术网

C# ListBox无法显示BindingList数据,而是显示对象名称

C# ListBox无法显示BindingList数据,而是显示对象名称,c#,listbox,C#,Listbox,所以我正在尝试制作一个windows窗体应用程序,我想制作一个事务windows窗体 所以我想创建一种购物车,每当用户按下“添加”按钮时,它就会出现在列表框中 我尝试使用BindingList将购物车显示到listbox,问题是listbox本身显示BindingList项的对象 代码 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using Syst

所以我正在尝试制作一个windows窗体应用程序,我想制作一个事务windows窗体

所以我想创建一种购物车,每当用户按下“添加”按钮时,它就会出现在列表框中

我尝试使用BindingList将购物车显示到listbox,问题是listbox本身显示BindingList项的对象

代码

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Pertemuan03
{
    public partial class Transaction : Form
    {
        class Cart{
           public string ProductID { get; set; }
            public string name { get; set; }
            public int quantity  { get; set; }

        };
        BindingList<Cart> shoppingList = new BindingList<Cart>();
        public Transaction()
        {
            InitializeComponent();
            listBox1.FormattingEnabled = true;
            listBox1.DataSource = shoppingList;


        }

        private void button1_Click(object sender, EventArgs e)
        {
            shoppingList.Add(new Cart {ProductID = comboBox1.SelectedItem.ToString(),name= textBox1.Text,quantity = (int)numericUpDown1.Value});
            listBox1.FormattingEnabled = true;
            listBox1.DataSource = null;
            listBox1.DataSource = shoppingList;
        }


    }
}
使用系统;
使用System.Collections.Generic;
使用系统组件模型;
使用系统数据;
使用系统图;
使用System.Linq;
使用系统文本;
使用System.Windows.Forms;
命名空间Pertemuan03
{
公共部分类事务:表单
{
班车{
公共字符串ProductID{get;set;}
公共字符串名称{get;set;}
公共整数数量{get;set;}
};
BindingList shoppingList=新建BindingList();
公开交易()
{
初始化组件();
listBox1.FormattingEnabled=true;
listBox1.DataSource=购物清单;
}
私有无效按钮1\u单击(对象发送者,事件参数e)
{
添加(新购物车{ProductID=comboBox1.SelectedItem.ToString(),name=textBox1.Text,quantity=(int)numericUpDown1.Value});
listBox1.FormattingEnabled=true;
listBox1.DataSource=null;
listBox1.DataSource=购物清单;
}
}
}
是这样的吗

如果我答对了您的问题,您必须设置display member属性。(cfr)

BindingList testList=newbindingList();
Add(newtesttype(){NameToDisplay=“MyName”});
myListBox.DataSource=testList;
myListBox.DisplayMember=“NameToDisplay”;

您应该通过为对象类提供适当的ToString方法来帮助列表框显示您的对象!重写购物车中的ToString方法,以您希望显示的方式格式化信息。如果我是对的,您将看到“object”的默认“ToString”行为。另一个选项可能是将数据源设置为对象的特定属性。对此不太确定,但这取决于您希望对象在列表框中呈现的程度。
        BindingList<TestType> testList = new BindingList<TestType>();
        testList.Add(new TestType() { NameToDisplay = "MyName" });
        myListBox.DataSource = testList;
        myListBox.DisplayMember = "NameToDisplay";