微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

c# – 如何将新项添加到绑定到实体的组合框?

由C#生成的设计类:

// 
    // usepurposeComboBox
    // 
    this.usepurposeComboBox.DataSource = this.usepurposeBindingSource;
    this.usepurposeComboBox.displayMember = "Name";
    this.usepurposeComboBox.FormattingEnabled = true;
    this.usepurposeComboBox.Location = new System.Drawing.Point(277,53);
    this.usepurposeComboBox.Name = "usepurposeComboBox";
    this.usepurposeComboBox.Size = new System.Drawing.Size(218,21);
    this.usepurposeComboBox.TabIndex = 4;
    this.usepurposeComboBox.ValueMember = "id";
    // 
    // usepurposeBindingSource
    // 
    this.usepurposeBindingSource.DataSource = typeof(mydatabaseEntities.usepurpose);

然后我将BindingSource(usepurposeBindingSource)绑定到实体:

usepurposeBindingSource.DataSource = mydatabaseEntities.usepurposes;

并且我无法向usepurposeComboBox添加新行,因为它已被绑定.有解决方法吗?

解决方法

最简单的方法是在dataTable中添加一个新行,然后将你的comboBox绑定到它,如下所示:

公司comps = new Company();

//pupulate dataTable with data
        DataTable DT = comps.getCompaniesList();

        //create a new row
        DaTarow DR = DT.NewRow();
        DR["c_ID"] = 0;
        DR["name"] = "Add new Company";
        DR["country"] = "IR";

        //add new row to data table
        DT.Rows.Add(DR);

        //Binding DataTable to cxbxCompany
        cxbxCompany.DataSource = DT;
        cxbxCompany.displayMember = "name";
        cxbxCompany.ValueMember = "c_ID";

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐