我曾尝试在MSDN论坛上发布此问题,但是,我没有成功。
我正在尝试为管理员工的企业制作C#Windows窗体应用程序。该应用程序使用Access 2010数据库。编辑员工页面是我遇到的麻烦。我希望员工能够从以下格式的组合框中选择:
FirstName LastName,EmployeePosition
数据库名称为EmployeeInformation.accdb 我想从数据库中获得的字段是:
组合框的名称是cboSelectEmp
我用来尝试获取此数据的当前代码是
private void LoadDataToCbo()
{
string connString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=EmployeeInformation.accdb";
// string query = @"SELECT FirstName from Employees";
string query =
@"SELECT EmpID, LastName + ', ' + FirstName + ' (' + EmployeePosition + ')' as Name FROM Employees";
OleDbDataAdapter dAdapter = new OleDbDataAdapter(query, connString);
DataTable source = new DataTable();
dAdapter.Fill(source);
cboSelectEmp.DataSource = source;
//cboSelectEmp.ValueMember = "FirstName";
//cboSelectEmp.DisplayMember = "FirstName";
cboSelectEmp.DisplayMember = "Name";
cboSelectEmp.ValueMember = "EmpID";
}
答案 0 :(得分:1)
您无法在组合框中显示多个列,但尝试使用
之类的查询select EmpID, LastName + ', ' + FirstName + ' (' + EMployeePosition + ')' as Name from Employees
然后设置你的组合框
cboSelectEmp.DataSource = dt; //dt is the datatable where the result of the query resides
cboSelectEmp.DisplayMember = "Name";
cboSelectEmp.ValueMember = "EmpID";
答案 1 :(得分:0)
试试这个
private void LoadDataToCbo()
{
string connString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=EmployeeInformation.accdb";
// string query = @"SELECT FirstName from Employees";
string query =
@"SELECT EmpID, LastName & ', ' + FirstName & ' (' & EmployeePosition & ')' as Name FROM Employees";
OleDbDataAdapter dAdapter = new OleDbDataAdapter(query, connString);
DataTable source = new DataTable();
dAdapter.Fill(source);
cboSelectEmp.DataSource = source;
//cboSelectEmp.ValueMember = "FirstName";
//cboSelectEmp.DisplayMember = "FirstName";
cboSelectEmp.DisplayMember = "Name";
cboSelectEmp.ValueMember = "EmpID";
}