如何使用DAL中的方法使用数据库值填充组合框

时间:2015-02-18 11:33:03

标签: c# combobox

我有一个名为Departments的表,其中我希望填充我的组合框的列名为DeptId。下面是我在我的DAL类中创建的数据表,但我不知道如何将这些值放入我的form1类中的deptCombobox。我试图将它附加到名为refreshButton_Click的按钮,但无论我尝试什么

都无效
          //DAL

 public DataTable fillDeptCombo()
    {
        SqlConnection con = new SqlConnection(@"Data Source=");

        con.Open();
        string queryText = "SELECT * FROM DEPARTMENT ";
        SqlCommand cmd = new SqlCommand(queryText, con);


        da = new SqlDataAdapter(cmd);
        dt = new DataTable();
        da.Fill(dt);
        con.Close();
        return dt;

        }

       //controller
    public DataTable fillDeptCombo()
    {
        return dal.fillDeptCombo();
    }

非常感谢任何帮助

1 个答案:

答案 0 :(得分:0)

您提到过“form1”,我认为它适用于Windows窗体。 在你的form1 cs代码中,这应该这样做。

DAL dl=new DAL()
Datatable dt=DAL.fillDeptCombo()
combobox.datasource=dt;
combobox.valuemember="deptID" //replace this with your value
combobox.displayMember="DeptName" //replace this with your text