0

我有这个在组合框中添加每个员工的全名。问题是我将在哪里存储(隐藏)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();
            }
        }
    }
4

2 回答 2

3

像这样做

using (SqlConnection con = new SqlConnection(myConnectionString.ConnectionString))
    {
        string query = "Select EmployeeID, (LastName + ','+ FirstName+' '+ MiddleName) as Name from Employee";
        SqlDataAdapter dap = new SqlDataAdapter(query ,con);
        DataTable dt = new DataTable();
        dap.Fill(dt);
         comboBox1.DisplayMember = "Name";
         comboBox1.ValueMember = "EmployeeID";
        DataRow dr = dt.NewRow();
        dr[0] = -1;
        dr[1] = "Add Employee";
        dt.Rows.InsertAt(dr, 0);
        comboBox1.DataSource = dt;

    }

然后

   private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
   {
       if(Convert.ToInt32(comboBox1.SelectedValue.ToString())==-1)
       {
        EmployeeForm nf = new EmployeeForm();
        DialogResult res = nf.ShowDialog();
        if (res == DialogResult.OK)
        {
            comboBox1.Items.Clear();
            fillComboBox();
        }
       }
    }

注意:如果您只想在用户更改组合框中的选定项目时做出反应,那么最好订阅 SelectionChangeCommitted。

于 2013-08-12T12:06:44.740 回答
0

首先我会创建一个类Employee。例如像这样:

public class Employee
{
    public int EmployeeID {get;set;}
    public string LastName { get; set; }
    public string FirstName { get; set; }
    public string MiddleName { get; set; }
    public string FullName { get { return LastName + ", " + FirstName + " " + MiddleName; } }

    public Employee(IDataRecord record)
    {
        this.EmployeeID = Convert.ToInt32(record["EmployeeID"]);
        this.LastName = record["LastName"].ToString();
        this.FirstName = record["FirstName"].ToString();
        this.MiddleName = record["MiddleName"].ToString();
    }
}

使用该类,您的fillBox()函数将如下所示:

 public void fillComboBox()
    {
        comboBox1.DisplayMember = "FullName";
        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())
                    {
                        comboBox1.Items.Add(new Employee(sqlreader));
                    }
                }
            }
        }
    }

当然 SelectedIndexChangedEvent 然后:

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
      {
          var selecetedEmployee = comboBox1.SelectedItem as Employee;
          if (selecetedEmployee == null)
          {
               EmployeeForm nf = new EmployeeForm();
               DialogResult res = nf.ShowDialog();
               if (res == DialogResult.OK)
               {
                   comboBox1.Items.Clear();
                   fillComboBox();
               }
           }
           else
           {
                textBox1.Text = selecetedEmployee.EmployeeID.ToString();
           }
      }
于 2013-08-12T12:54:47.533 回答