简体   繁体   English

comboBox_TextChanged上的combobox.selectedvalue不能正常工作

[英]combobox.selectedvalue on comboBox_TextChanged isn't working properly

I have this C# code, and I'm using Sqlite 我有此C#代码,并且我正在使用Sqlite

private void Form1_Load(object sender, EventArgs e)
    {
        using (var db = new mainEntities())
        {
            comboBox1.DataSource = db.Employers.ToList();
            comboBox1.DisplayMember = "Surname";
            comboBox1.ValueMember = "ID";

            //convert to long
            long id = Convert.ToInt64(comboBox1.SelectedValue);
            //get employer's ID
            var employer = (from s in db.Employers
                            where s.ID == id
                            select s).FirstOrDefault();
            //set 2 textboxes to Name and Surname values
            TextBox1.Text = employer.Name;
            TextBox2.Text = employer.Surname;
            db.SaveChanges();

        }
    }

when I try to set my 2 textboxes changing the values of my combobox with this code: 当我尝试通过以下代码设置2个文本框更改组合框的值时:

private void comboBox1_TextChanged(object sender, EventArgs e)
    {

        long id = Convert.ToInt64(comboBox1.SelectedValue);
            using (var db = new mainEntities())
        {
            var employer = (from s in db.Employers
                            where s.ID == id
                            select s).FirstOrDefault();
            //set 2 textboxes to Name and Surname values
            TextBox1.Text = employer.Name;
            TextBox2.Text = employer.Surname;       
        }

I get: System.InvalidCastException: 'System.Data.Entity.DynamicProxies.Employers_4603A50E3C8B2711C02C0DAE379C7EC403103DB450E8CCC08E7874FDC1318E90' on type 'System.IConvertible'.' 我得到:System.InvalidCastException:'System.Data.Entity.DynamicProxies.Employers_4603A50E3C8B2711C02C0DAE379C7EC403103DB450E8CCC08E7874FDC1318E90'类型为'System.IConvertible'。'

If I disable Proxy Creation with this: 如果我用此禁用代理创建:

this.Configuration.ProxyCreationEnabled = false;

in my Db.context.cs I get: System.InvalidCastException: 'Unable to execute cast on objects of type 'Db.Employers' on type 'System.IConvertible'.' 在我的Db.context.cs中,我得到:System.InvalidCastException:'无法对类型为'System.IConvertible'的类型为'Db.Employers'的对象执行强制转换。

If I try to convert to String instead of long and put the string in a messagebox I get 2 times 'Db.Employers' and from the third the numerical values of the ID as it would be in my combobox. 如果我尝试转换为String而不是long并将字符串放在消息框中,则得到的值是“ Db.Employers”的2倍,而从ID的第三个值开始,它就是组合框中的数值。 Where am I wrong? 我哪里错了?

private void Form1_Load(object sender, EventArgs e)
{
    using (var db = new mainEntities())
    {
        comboBox1.DataSource = db.Employers.ToList();
        comboBox1.ValueMember = "ID";
        comboBox1.DisplayMember = "Surname";
        comboBox1.SelectedIndex =0;
        //convert to long
        long id = Convert.ToInt64(comboBox1.SelectedValue);
        //get employer's ID
        var employer = (from s in db.Employers
                        where s.ID == id
                        select s).FirstOrDefault();
        //set 2 textboxes to Name and Surname values
        TextBox1.Text = employer.Name;
        TextBox2.Text = employer.Surname;
        //db.SaveChanges();

    }
}

if you want change the text value to relate with selected value from comboBox you can do it by SelectedIndexChanged Event 如果您想更改文本值以与comboBox中的选定值相关,则可以通过SelectedIndexChanged事件来实现

private void ComboBox1_SelectedIndexChanged(object sender,System.EventArgs e)
  {
    long id = Convert.ToInt64(comboBox1.SelectedValue);
        using (var db = new mainEntities())
    {
        var employer = (from s in db.Employers
                        where s.ID == id
                        select s).FirstOrDefault();
        //set 2 textboxes to Name and Surname values
        TextBox1.Text = employer.Name;
        TextBox2.Text = employer.Surname;       
    }
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM