如何在Wpf中将数据从SQL Server数据库表显示到组合框

本文关键字:数据库 Server 显示 组合 SQL Wpf 数据 | 更新日期: 2023-09-27 17:53:21

我想在Wpf/c#中显示从SQL Server数据库表到组合框的数据。

目前我有一个表Lookup_Type,包含两列,SectorNumberDescription

假设该表中存储的数据如下:

SectorNumber   Description
---------------------------
     01        Antitrust
     02        Civil Rights
     03        Criminal
     04        Tax
     ...

我有一个文本块和一个组合框在我的主窗口。xaml文件:

<Textblock Text="Type of Justice Agencies: " Name="TypeTextBlock" ... />
<Combobox Name="TypeComboBox" 
          Loaded="ComboBox_Loaded" 
          SelectionChanged="ComboBox_SelectionChanged"
          ... />

如何在c#中显示SQL Server数据库表中的数据到组合框?

我希望组合框项读作01, Antitrust, 02, Civil Rights等。谢谢。

如何在Wpf中将数据从SQL Server数据库表显示到组合框

Here example:      
var dict = new Dictionary<int, string>();
dict.Add(1, "01, Antitrust");
dict.Add(2, "02, Civil Rights");
dict.Add(3, "03, Criminal");
dict.Add(4, "04, Tax");
myCombobox.DataSource = new BindingSource(dict, null);
myCombobox.DisplayMember = "Value";
myCombobox.ValueMember = "Key";