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

时间:2011-10-03 08:14:54

标签: c# entity-framework binding combobox row

由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)绑定到Entities:

usepurposeBindingSource.DataSource = mydatabaseEntities.usepurposes;

我无法在usepurposeComboBox中添加新行,因为它已被绑定。有解决方法吗?

5 个答案:

答案 0 :(得分:1)

最简单的方法是在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";

答案 1 :(得分:0)

我假设您要添加例如有时称为“选择一个”的第一个项目,就好像您想要实时数据一样,您应该只看到数据的来源并向该“表格”添加更多项目。 / p>

this.usepurposeBindingSource是一个对象......为什么不在绑定之前加入它?

如果它是List<T>,这将是正常的

this.usepurposeBindingSource.Insert(0, new T() {
    Name = "Choose one",
    Id = ""
});

然后Bind()它......

记住来验证,因为它是一个字符串,不会是你想要的对象


这是有效的:

List<MyObject> usepurposeBindingSource { get; set; }

private void FillUpData()
{
    // Simulating an External Data
    if (usepurposeBindingSource == null || usepurposeBindingSource.Count == 0)
    {
        this.usepurposeBindingSource = new List<MyObject>();
        this.usepurposeBindingSource.Add(new MyObject() { Name = "A", ID = 1 });
        this.usepurposeBindingSource.Add(new MyObject() { Name = "B", ID = 2 });
        this.usepurposeBindingSource.Add(new MyObject() { Name = "C", ID = 3 });
    }
}

private void FillUpCombo()
{
    FillUpData();

    // what you have from design
    // comment out the first line
    //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";

    // to do in code:

    this.usepurposeBindingSource.Insert(0, new MyObject() { Name = "Choose One", ID = 0 });

    // bind the data source
    this.usepurposeComboBox.DataSource = this.usepurposeBindingSource;

}

诀窍是注释掉DataSource行并在代码中执行此操作,一个元素插入到模型中的对象中

//this.usepurposeComboBox.DataSource = this.usepurposeBindingSource;

答案 2 :(得分:0)

最简单的方法是使用某种“ViewModel”包装BindingSource。新类将返回一个“完整”项目列表 - 包括原始绑定源提供的项目,以及那些“其他”项目。

然后,您可以将新包装器绑定到组合框。

关于这一段时间我写了一篇article ...这不是我最好的作品,它可能有点过时,但它应该让你到那里。

答案 3 :(得分:0)

我自己解决了。我创建了一个新的未绑定组合框,然后将其绑定到数据表。不确定它是否是最好的方式,但它适用于我。感谢您的所有建议。 :)

private void FillCombobox()
        {
            using (mydatabaseEntities mydatabaseEntities = new mydatabaseEntities())
            {
                List<usepurpose> usepurposes = mydatabaseEntities.usepurposes.ToList();              
                DataTable dt = new DataTable();
                dt.Columns.Add("id");
                dt.Columns.Add("Name");
                dt.Rows.Add(-1, "test row");
                foreach (usepurpose usepurpose in usepurposes)
                {
                    dt.Rows.Add(usepurpose.id, usepurpose.Name);
                }
                usepurposeComboBox.ValueMember = dt.Columns[0].ColumnName;
                usepurposeComboBox.DisplayMember = dt.Columns[1].ColumnName;
                usepurposeComboBox.DataSource = dt;
            }
        }

答案 4 :(得分:0)

请查看此链接:http://forums.asp.net/t/1695728.aspx/1

在asp中你可以添加它来插入一个空行:

    <asp:DropDownList ID="CustomerDropDownList" runat="server"
    DataSourceID="CustomerEntityDataSource" DataTextField="CustomerId"
    DataValueField="CustomerId" AppendDataBoundItems="true">
    <asp:ListItem Text="Select All Customers" Value="" />
    </asp:DropDownList>

或代码背后:

    DropDownList1.AppendDataBoundItems = true;
    DropDownList1.Items.Add(new ListItem("Select All Customers", "-1"));