加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-15 21:24:17 所属栏目:百科 来源:网络整理
导读:由C#生成的设计类: // // usepurposeComboBox // this.usepurposeComboBox.DataSource = this.usepurposeBindingSource; this.usepurposeComboBox.DisplayMember = "Name"; this.usepurposeComboBox.FormattingEnabled = true; this.usepurposeComboBox.Loc
由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";

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读