我有这个在组合框中添加每个员工的全名。问题是我将在哪里存储(隐藏)EmpID,以便如果在组合框中选择了一个项目,它将在文本框中显示所选员工的 EmpID?
public void fillComboBox()
{
comboBox1.Items.Add("Add Employee");
using (SqlConnection myDatabaseConnection = new SqlConnection(myConnectionString.ConnectionString))
{
myDatabaseConnection.Open();
using (SqlCommand mySqlCommand = new SqlCommand("Select EmployeeID, LastName, FirstName, MiddleName from Employee", myDatabaseConnection))
{
using (SqlDataReader sqlreader = mySqlCommand.ExecuteReader())
{
while (sqlreader.Read())
{
string Lname = sqlreader.GetString(sqlreader.GetOrdinal("LastName"));
string Fname = sqlreader.GetString(sqlreader.GetOrdinal("FirstName"));
string Mname = sqlreader.GetString(sqlreader.GetOrdinal("MiddleName"));
string fullName = Lname + ", " + Fname + " " + Mname;
comboBox1.Items.Add(fullName);
}
}
}
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
//textBox1.Text = SelectedEmpID
if (comboBox1.Text == "Add Employee")
{
EmployeeForm nf = new EmployeeForm();
DialogResult res = nf.ShowDialog();
if (res == DialogResult.OK)
{
comboBox1.Items.Clear();
fillComboBox();
}
}
}