从MS Access中使用Linq填充ComboBox

时间:2017-02-18 14:00:24

标签: c# linq ms-access combobox

我是C#和Linq的新手,想要从MS Access中使用Linq填充ComboBox。但是,如果我调试代码,ComboBox为空。

我读过一些较旧的帖子,但他们没有帮助。失败在哪里?

private void Form1_Load(object sender, EventArgs e)
{
    try
    {
        connection.Open();
        label1.Text = "OK";

        PoolDataSet pool = new PoolDataSet();

        comboBoxPool.DisplayMember = "Pool-Name";
        comboBoxPool.ValueMember = "ID";

        comboBoxPool.DataSource = (from x in pool.Pools
                                      select x._Pool_Name).ToList();

        connection.Close();
    }
    catch (Exception ex)
    {
        MessageBox.Show("Error" + ex);
    }
 }

1 个答案:

答案 0 :(得分:0)

谢谢,填充我的池 - 对象是问题所在。我通过添加.Fill():

得到它

connection.Open();
connectionLabel.Text = "Connection successful!";

PoolsDataSet pool = new PoolsDataSet();
(new PoolsDataSetTableAdapters.PoolsTableAdapter()).Fill(pool.Pools);
comboBoxPool.DataSource = (from x in pool.Pools
                           select x.ID).ToList();

comboBoxPool.DisplayMember = "ID";

connection.Close();

正在运行;)