2011-10-03 58 views
4

这是由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。有没有解决方法?

回答

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; 
      } 
     } 
0

我假设你想添加例如有时称为“选择一个”的第一个项目,就好像你想要实时生成数据一样,你应该看到数据来自哪里,并向该“表格”添加更多项目。

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;

+1

它不能这样做。 System.Windows.Forms.dll中发生未处理的异常类型'System.ArgumentException' 其他信息:设置DataSource属性时无法修改Items集合。 – JatSing

+0

为什么不添加到'this.usepurposeBindingSource'? – balexandre

0

做到这一点的最简单方法,就是用某种“ViewModel”来包装你的BindingSource。新课程将返回一个“完整”项目列表 - 既包括原始绑定源提供的项目,也包括那些“额外”项目。

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

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

1

的捷径是一个新行添加到您的DataTable,并那么你的组合框绑定到它是这样的:

公司谱曲=新的公司();

 //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"; 
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")); 
+0

AppendDataBoundItems属性似乎只适用于DropDownLists而不是Comboxes。 – newenglander

相关问题