gpt4 book ai didi

c# - 如何向绑定(bind)到实体的组合框添加新项目?

转载 作者:行者123 更新时间:2023-11-30 12:51:08 27 4
gpt4 key购买 nike

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) 绑定(bind)到实体:

usepurposeBindingSource.DataSource = mydatabaseEntities.usepurposes;

而且我无法向 usepurposeComboBox 添加新行,因为它已被绑定(bind)。有解决方法吗?

最佳答案

最短的方法是向您的 dataTable 添加一个新行,然后像这样将您的组合框绑定(bind)到它:

公司补偿 = 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";

关于c# - 如何向绑定(bind)到实体的组合框添加新项目?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7632540/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com