4

我正在创建一个从数据库中提取数据并提供两个组合框的表单。ComboBox1 以“名字姓氏”格式显示员工姓名。ComboBox2 以“Last, First”格式显示员工姓名。这些名字毫无困难地被拉进组合框。

我试图弄清楚如何做的是,一旦用户从任一下拉框中选择一个名称,它将根据来自查询的其他信息填充某些文本框,例如 textBox1、textBox2。

我试图使用

textBox1.Text = comboBox1.SelectedItem;

但是,我收到一条错误消息:无法将类型“对象”隐式转换为“字符串”。存在显式转换(您是否缺少演员表?)

这是我当前的代码。我故意省略了对数据库的连接查询等。

public partial class Employees : Form
{
    public Employees()
    {
        InitializeComponent();

        //Connect to database for Employees Table Headers
        SqlConnection myConnection = new SqlConnection(@"Server=Server...");

        try {
            myConnection.Open();
            string SqlDataPull = String.Format("SELECT * FROM Employees WHERE Lname IS NOT NULL {0} ORDER By Lname", (checkBox1.Checked ? "AND Active='Y'" : ""));
            SqlCommand cmd = new SqlCommand(SqlDataPull, myConnection);
            cmd.CommandType = CommandType.Text;
            SqlDataReader dr = cmd.ExecuteReader();

            while (dr.Read()) {
                string strEmployee = String.Format("{0}  {1}", dr["Fname"], dr["Lname"]);
                comboBox1.Items.Add(strEmployee);
                textBox1.Text = comboBox1.SelectedItem;
                string strEmployee2 = String.Format("{0}, {1}", dr["Lname"], dr["Fname"]);
                comboBox2.Items.Add(strEmployee2);
            }
        } catch (Exception e) {
            MessageBox.Show(e.ToString());
        } finally {
            if (myConnection != null) {
                myConnection.Dispose();
            }
        }
    }

    private void button1_Click(object sender, EventArgs e)
    {
        Main myNewForm = new Main();
        myNewForm.Show();
        this.Close();
    }

    private void button2_Click(object sender, EventArgs e)
    {
        Reports myNewForm = new Reports();
        myNewForm.Show();
        this.Close();
    }
}
4

6 回答 6

5

如果您只需要文本,您可以简单地使用:

textBox1.Text = comboBox1.Text;
于 2012-07-20T15:46:39.620 回答
5

在 SelectedIndexChanged 事件中,添加代码:

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
  if (comboBox1.SelectedIndex == -1) {
    textBox1.Text = string.Empty;
  } else {
    textBox1.Text = comboBox1.SelectedItem.ToString();
  }
}  

您可能应该从您的 while 循环中删除此语句:

// textBox1.Text = comboBox1.SelectedItem;

因为您刚刚将该项目添加到 ComboBox 项目集合中,但您实际上并不知道它已被选中。

另外,请确保将事件连接起来:

public Employees()
{
  InitializeComponent();

  // yada-yada-yada

  comboBox1.SelectedIndexChanged += comboBox1_SelectedIndexChanged;
}
于 2012-07-20T15:49:42.630 回答
3

您需要的是使用 TEXT 值而不是textBox1.Text = comboBox1.SelectedItem;It should betextBox1.Text = comboBox1.SelectedItem.ToString();或简单地textBox1.Text = comboBox1.Text

于 2012-07-20T15:45:24.957 回答
3

ComboBox由于您可以使用任何类型的项目(不仅仅是string)填充 a ,因此该SelectedItem属性的类型为object。如果您插入strings,您可以通过投射来检索它们

string s = (string)myComboBox.SelectedItem;

如果您添加了其他类型的项目,例如类Person,您可以检索此对象或获取其字符串或某些属性

Person p = (Person)myComboBox.SelectedItem;
string s = myComboBox.SelectedItem.ToString();
string lastName = ((Person)myComboBox.SelectedItem).LastName;
于 2012-07-20T15:53:46.857 回答
2

不幸的是,你不能这样做:

textBox1.Text = comboBox1.SelectedItem.ToString();

因为结果也是路径:

System.Windows.Controls.ComboBoxItem: Citroen

你只需要价值例如雪铁龙

所以问题可以通过搜索这样的字符串来解决:

      string auto = comboBox1.SelectedItem.ToString();
      int index = auto.IndexOf(" ");
      carModel.Text = auto.Substring(index+1);
于 2012-12-29T13:22:23.727 回答
-2

你需要使用:

textBox1.Text = comboBox1.SelectedItem.Text获取项目的文本部分。

SelectedItem是一个对象,您正试图将其分配给Text文本框的属性。

于 2012-07-20T15:48:38.693 回答