我有tblDepartments
带列的表DeptID, Department, Description
。我也有下拉框drpDepartments
。我想Department
在下拉框中显示列的内容。我尝试将此 C# 代码用于 winforms,但它不起作用:
drpDepartments.DataSource = dsDep.Tables["tblDepartment"];
drpDepartments.DisplayMember = "Department";
drpDepartments.ValueMember = "DeptID";
drpDepartments.Text = "Choose Department";
我如何使用 ASP.NET C# 来做到这一点?谢谢。
新代码(仍然无法正常工作)
sConn = new SqlConnection(sStr);
daEmp = new SqlDataAdapter("SELECT * FROM tblEmployee", sConn);
daDep = new SqlDataAdapter("SELECT * FROM tblDepartment", sConn);
dsEmp = new DataSet();
dsDep = new DataSet();
daEmp.Fill(dsEmp, "tblEmployee");
daDep.Fill(dsDep, "tblDepartment");
dsEmp.Tables["tblEmployee"].PrimaryKey = new DataColumn[] { dsEmp.Tables["tblEmployee"].Columns["EmployeeID"] };
DataTable dt=new DataTable();
daDep.Fill(dt);
drpDepartments.DataTextField = "Department";
drpDepartments.DataValueField = "DeptID";
drpDepartments.DataSource = dt;
drpDepartments.DataBind();