Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/visual-studio/8.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# 用mysql c中的数据列表填充组合框#_C#_Mysql_Dapper - Fatal编程技术网

C# 用mysql c中的数据列表填充组合框#

C# 用mysql c中的数据列表填充组合框#,c#,mysql,dapper,C#,Mysql,Dapper,我有这个方法 public List<Trgovina> getAllStores() { using (IDbConnection connection = new MySqlConnection(Helper.CnnVal("dbConn"))) { return connection.Query<Trgovina>("TrgovineViewAll", null, commandType: Comman

我有这个方法

public List<Trgovina> getAllStores()
{
    using (IDbConnection connection = new MySqlConnection(Helper.CnnVal("dbConn")))
    {
        return connection.Query<Trgovina>("TrgovineViewAll", null,
                commandType: CommandType.StoredProcedure
            )
            .ToList();
    }
}
公共列表getAllStores() { 使用(IDbConnection connection=newmysqlconnection(Helper.CnnVal(“dbConn”)) { 返回connection.Query(“TrgovineViewAll”,null, commandType:commandType.StoredProcess ) .ToList(); } } 它从数据库中获取所有存储信息。它在启动时调用的方法

public MainWindow()
{
    InitializeComponent();

    List<Trgovina> stores = new List<Trgovina>();
    stores = da.getAllStores();
    comboxStoreNames.ItemsSource = stores;
}
public主窗口()
{
初始化组件();
列表存储=新列表();
stores=da.getAllStores();
comboxStoreNames.ItemsSource=stores;
}

存储中充满了数据。虽然数据不是我需要的格式。它只显示
{Project\u Budget.Engine.Trgovina}
。我需要的信息就在里面。如何以字符串格式从那里获取数据,以便在组合框中显示它?

您必须使用组合框的
DisplayMember
ValueMember
属性,这允许您绑定所需的属性。

您必须使用组合框的
DisplayMember
ValueMember
属性,允许您绑定所需的属性。

您可以覆盖
ToString

public override string ToString() => ThePropertyOrExpressionToBeDisplayed;
或者,如果使用数据绑定,请设置组合框的
DisplayMember
ValueMember
属性<代码>显示成员是要显示的属性
ValueMember
通常是一个Id或键属性。您可以通过
SelectedValue
访问后者,通过
SelectedItem
获取整个对象,并通过
SelectedIndex
获取其索引


请注意,在您的示例中,组合框使用的是
ToString
;但是,由于继承自
System.Object
的默认实现是返回类型名,因此您没有得到足够的显示。

您可以覆盖
ToString

public override string ToString() => ThePropertyOrExpressionToBeDisplayed;
或者,如果使用数据绑定,请设置组合框的
DisplayMember
ValueMember
属性<代码>显示成员是要显示的属性
ValueMember
通常是一个Id或键属性。您可以通过
SelectedValue
访问后者,通过
SelectedItem
获取整个对象,并通过
SelectedIndex
获取其索引


请注意,在您的示例中,组合框使用的是
ToString
;但是,由于继承自
System.Object
的默认实现是返回类型名,因此您没有得到足够的显示。

为什么不使用datasource和DisplayMember以及ValueMember?为什么不使用datasource、DisplayMember和ValueMember?