I have tried posting this question on MSDN forums, however, i had no success.
我曾尝试在MSDN论坛上发布此问题,但是,我没有成功。
I am trying to make a C# Windows Forms Application for a business that will manage its employees. The application uses an Access 2010 Database. The edit Employee page is what I am having trouble with. I want the Employees to be able to be selected from a combobox in the format of:
我正在尝试为管理其员工的企业制作C#Windows窗体应用程序。该应用程序使用Access 2010数据库。编辑员工页面是我遇到的麻烦。我希望员工能够从以下格式的组合框中选择:
FirstName LastName, EmployeePosition
FirstName LastName,EmployeePosition
The database name is EmployeeInformation.accdb The fields that I want from the Database are:
数据库名称是EmployeeInformation.accdb我想从数据库中获取的字段是:
- FirstName
- LastName
- EmployeePosition
The name of the Combo Box is cboSelectEmp
组合框的名称是cboSelectEmp
The current code i am using to try and get this data is
我用来尝试获取此数据的当前代码是
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";
}
2 个解决方案
#1
0
Try this
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";
}
#2
1
You can't display multiple columns in combobox but try to use a query like
您无法在组合框中显示多个列,但尝试使用类似的查询
select EmpID, LastName + ', ' + FirstName + ' (' + EMployeePosition + ')' as Name from Employees
then set your combobox
然后设置你的组合框
cboSelectEmp.DataSource = dt; //dt is the datatable where the result of the query resides
cboSelectEmp.DisplayMember = "Name";
cboSelectEmp.ValueMember = "EmpID";
#1
0
Try this
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";
}
#2
1
You can't display multiple columns in combobox but try to use a query like
您无法在组合框中显示多个列,但尝试使用类似的查询
select EmpID, LastName + ', ' + FirstName + ' (' + EMployeePosition + ')' as Name from Employees
then set your combobox
然后设置你的组合框
cboSelectEmp.DataSource = dt; //dt is the datatable where the result of the query resides
cboSelectEmp.DisplayMember = "Name";
cboSelectEmp.ValueMember = "EmpID";