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?