【问题标题】:Fill Combobox with list of data from mysql c#用来自mysql c#的数据列表填充组合框
【发布时间】:2018-10-06 15:00:13
【问题描述】:

我有这个方法

public List<Trgovina> getAllStores()
{
    using (IDbConnection connection = new MySqlConnection(Helper.CnnVal("dbConn")))
    {
        return connection.Query<Trgovina>("TrgovineViewAll", null,
                commandType: CommandType.StoredProcedure
            )
            .ToList();
    }
}

它从数据库中获取所有商店信息。启动时调用的方法

public MainWindow()
{
    InitializeComponent();

    List<Trgovina> stores = new List<Trgovina>();
    stores = da.getAllStores();
    comboxStoreNames.ItemsSource = stores;
}

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

【问题讨论】:

标签: c# mysql dapper


【解决方案1】:

您可以覆盖ToString

public override string ToString() => ThePropertyOrExpressionToBeDisplayed;

或者,如果您使用数据绑定,请设置组合框的DisplayMember 和ValueMember 属性。 DisplayMember 是您要显示的属性。 ValueMember 通常是 Id 或键属性。您可以通过SelectedValue 访问后者,而您可以通过SelectedItem 获取整个对象,并通过SelectedIndex 获取其索引。

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

【讨论】:

    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 2014-03-10
      • 2019-01-07
      • 1970-01-01
      • 1970-01-01
      • 2014-09-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多