c# - 将 WPF 组合框与 Postgres 数据库值链接起来

标签 c# wpf postgresql combobox

我试图用来自 postgres 数据库的数据填充组合框,但我无法让它工作。

C# 代码:

private void fill_combo()
{
    string CommandText = "select ida from antenne";
    using (NpgsqlConnection sqlConn = new NpgsqlConnection("server=localhost;port=5432;user=postgres;pwd=password;database=BDTelecom"))
    {
        sqlConn.Open();
        NpgsqlCommand sqlCmd = new NpgsqlCommand(CommandText, sqlConn);
        NpgsqlDataAdapter da = new NpgsqlDataAdapter(sqlCmd);
        DataTable dt = new DataTable();
        da.Fill(dt);
        comboBox.Text = "ida";
    }
}

XAML 代码:

<ComboBox x:Name="comboBox"
          HorizontalAlignment="Left" Height="23" VerticalAlignment="Top" Width="170"
          ItemsSource="{Binding}" />

有什么想法吗?

最佳答案

假设数据表中的字段名称(数据库中的列名)为"ida",类型为string,则需要做这样的事情:

comboBox.ItemsSource =
    dt.Rows
    .Cast<DataRow>()
    .Select(x => x.Field<string>("ida"));

关于c# - 将 WPF 组合框与 Postgres 数据库值链接起来,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34779145/

相关文章:

wpf - 将资源或样式设置为来自不同类库的窗口

sql - 给定时间/间隔以计算每个分组数据中的开盘价/最高价/最低价/收盘价

json - 选择数组 JSONB 中的对象

c# - 如何简化在主窗体和子窗体之间传递的设置值的代码

c# - System.Windows.Controls.TextBox 句柄

wpf - 如何在面板中的另一个 WPF 窗口中加载一个 WPF (xaml) 窗口?

wpf - 以编程方式为 DataGrid 中的行分配颜色

c# - 预加载 Azure 网站以增加首次访问加载时间

c# - 使用 AppDomain 加载/卸载外部程序集

sql - 在单个语句中选择 "many to many"和 "belongs to"