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

m528fe3b  于 2021-06-18  发布在  Mysql
关注(0)|答案(2)|浏览(349)

我有这个方法

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;
}

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

dwbf0jvd

dwbf0jvd1#

您可以覆盖 ToString ```
public override string ToString() => ThePropertyOrExpressionToBeDisplayed;

或者,如果使用数据绑定,请设置 `DisplayMember` 以及 `ValueMember` 组合框的属性。 `DisplayMember` 是要显示的属性。 `ValueMember` 通常是id或键属性。你可以通过 `SelectedValue` 当你能把整个物体穿过去的时候 `SelectedItem` 以及它的索引 `SelectedIndex` .
请注意,在您的示例中,组合框使用 `ToString` ; 但是,作为从 `System.Object` 是返回类型名,您没有得到足够的显示。
lmyy7pcs

lmyy7pcs2#

你必须使用 DisplayMember 以及 ValueMember 属性,允许您绑定所需的属性。

相关问题