2011-10-03 59 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

相關問題