c# - 我的 ComboBox3 无法将第二个数据类型转换为我的 textbox1

标签 c# mysql

大家好晚上好:D为什么我的另一个数据类型不能转换为textbox1.text我不知道为什么他不能转换但我的其他数据是正确的这是我的代码 DATABASE

       private void AddEmployee_Load(object sender, EventArgs e)
    {
        fillposition();
    }
//fill
    public void fillposition()
    {
        con.Open();
        MySqlDataReader dr;
        MySqlCommand cmd = new MySqlCommand("select * from position", con);
        dr = cmd.ExecuteReader();

        while (dr.Read())
        {
            string data = dr.GetString("EmpPosition");
            comboBox3.Items.Add(data);
        }
        con.Close();
    }

Form did not cast the prefix

    public void getposition()
    {
        if (Int32.TryParse(comboBox3.SelectedItem.ToString(), out tc))
        {
            con.Open();
            MySqlCommand cmd = new MySqlCommand("select * from position 
            where EmpPosition=@EmpPosition ", con);
            cmd.Parameters.Add("@EmpPosition", MySqlDbType.Int32).Value = 
            tc;
            dr = cmd.ExecuteReader();
            if (dr.Read())
            {
                textBox1.Text = dr["Prefix"].ToString();
            }
            con.Close();
        }
    }
   private void comboBox3_SelectedIndexChanged_1(object sender, EventArgs e)
    {
        getposition();
    }

最佳答案

1) 设置组合框 ValueMemberDisplayMember 属性。

public void fillposition()
{
    //Your code as it is
    con.Close();

    comboBox3.ValueMember = "Value";
    comboBox3.DisplayMember = "Text";
}

2) 然后在下面的if

中使用SelectedValue代替SelectedItem
if (Int32.TryParse(comboBox3.SelectedValue.ToString(), out tc))
{
   //You stuff here
}

关于c# - 我的 ComboBox3 无法将第二个数据类型转换为我的 textbox1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54593560/

相关文章:

mysql - 尝试修复 "tMySQL query error: Duplicate entry"对于 garrysmod 服务器

MySQL 和 PhP PayPal IPN 购物车

c# - 如何从类型名称和程序集名称加载类型

mysql - 如何从连接结果中排除 NULL 行

c# - 将日期时间转换为 double

c# - 通过 Javascript 更改时,HiddenField EventHandler ValueChanged 不会触发

mysql - 在 Mysql 中为多个根存储分层数据的最佳方法是什么?

mysql - 如何在 Spring Data JPA 排序中定义空处理?

c# - 消息队列异常 : Queue does not exist or you do not have sufficient permissions to perform the operation

c# - 查找枚举成员