【问题标题】:Retrieving value from combobox Winforms / Entity Framework从组合框 Winforms / Entity Framework 中检索值
【发布时间】:2018-04-21 06:43:05
【问题描述】:

我正在尝试用公司列表填充我的组合框,然后将所选公司的价值(其Id)输入我的数据库。

我有这门课CBItem:

public class CBItem
{
    public string Name { get; set; }
    public int Value { get; set; }

    public override string ToString()
    {
       return Name;
    }
}

我使用这个将项目添加到组合框:

using (var db = new DataContext())
{
    var list_of_companies = db.Companies;

    foreach (Company c in list_of_companies)
    {
        CBItem item = new CBItem();
        item.Name = c.Name;
        item.Value = c.Id;
        comboBox1.Items.Add(item);
    }
}

问题是当我想获得所选项目的价值时,我尝试过这样的事情:

new_person.Company.Id = (comboBox1.SelectedItem).Value;

当然不行:/有什么建议吗?

【问题讨论】:

  • 什么样的“行不通”?不编译?错误的价值?空?
  • 据我了解,我将 CBItem 类的对象添加到组合框列表中。这些对象应该具有我应该能够使用的属性名称和值。
  • 我得到这个信息:'object'不包含'Value'的定义,并且找不到接受'object'类型的第一个参数的扩展方法'Value'
  • 您可能需要将SelectedItem 返回的object 转换为CBItem
  • 我试过这个:new_person.Company.Id = (comboBox1.SelectedItem as CBItem).Value;但我得到: System.NullReferenceException: '对象引用未设置为对象的实例。'

标签: c# winforms entity-framework combobox


【解决方案1】:

你是对的,这行有问题: new_person.Company.Id 我不知道出了什么问题。我有模型 Person,它具有公司 ID 的外键。 也许我的数据库模型有问题。 我的人模型:

public class Person
    {
        public Person()
        {

        }
        public int PersonId { get; set; }
        public string Name { get; set; }
        public string Surname { get; set; }
        public string Job { get; set; }
        public int Phone { get; set; }
        public int Mobile { get; set; }

        public Company Company { get; set; }
    }

公司模式:

public class Company
    {
        public Company()
        {

        }
        public int Id { get; set; }
        public string Name { get; set; }
        public int NIP { get; set; }
        public string Address { get; set; }
        public string City { get; set; }
        public int Code { get; set; }
        public int Phone { get; set; }
        public string Notes { get; set; }

        public ICollection<Person> Person { get; set; }
    }

保存代码:

using (var db = new DataContext())
            {
                Person new_person = new Person();
                new_person.Name = textBox1.Text;
                new_person.Surname = textBox2.Text;
                new_person.Job = textBox3.Text;
                new_person.Phone = Int32.Parse(textBox4.Text);
                new_person.Mobile = Int32.Parse(textBox5.Text);
                new_person.Company.Id = (comboBox1.SelectedItem as CBItem).Value;
                db.Person.Add(new_person);
                db.SaveChanges();
                MessageBox.Show("Person successfully added!");
                this.Close();
            }

【讨论】:

    【解决方案2】:

    如果满足以下条件,您的代码可以正常工作:

    • new_person 不是 null
    • new_person.Company 不是 null
    • combobox.SelectedItem 不是 null

    您还必须像这样将combobox.SelectedItem 转换为CBItem:

    new_person.Company.Id = ((CBItem)comboBox1.SelectedItem).Value;
    

    作为站点注释而不是逐个添加每个项目,您可以通过将 ComboBox 通过其 DataSource 属性绑定到实现 IList 接口或 Array 的对象来归档相同的内容:

    comboBox1.DataSource = list_of_companies
        .Select(c => new CBItem {
            Name = c.Name,
            Value = c.Id
        })
        .ToArray();
    

    【讨论】:

    • 嗯,这对我有用:new_person.Company = db.Companies.Find(((CBItem)comboBox1.SelectedItem).Value);
    • 这不是必需的。您的代码会导致查询数据库中的表(或至少是表的缓存表示)以查找 id 等于 ComboBox 中选定元素的 id 的实体 - 但您已经在 comboBox1.SelectedItem 中引用了该实体。如果您想将实体分配给您的对象模型,您可以尝试:new_person.Company = (CBItem)comboBox1.SelectedItem;
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多