2017-01-14 97 views
1

我需要您關於C#datagridview的幫助。 我想從數據源生成一個datagridview。 數據網格視圖有4列。 列1:名字 列2:姓氏 列3:性別 列4:國家。 國家/地區列是組合框列。從數據源填充Datagridview中的Combobox列

我已經創建了相應的數據源並將數據源設置爲網格。 前三列正在生成,但未添加組合框。 這裏是我的應用程序的示例代碼

List<Mydataclass> dataclassList = new List<Mydataclass>(); 
for (int i = 0; i < 5; i++) 
     { 
      Mydataclass dataclass = new Mydataclass(); 
      dataclass.firstname = "firstname" + i; 
      dataclass.secondname = "second name" + i; 
      dataclass.gender = "gender" + i; 
      dataclass.country = new string[] { "BD", "AUS"}; 

      dataclassList.Add(dataclass); 

     } 
BindingSource bindingSource1 = new BindingSource(); 

     bindingSource1.DataSource = dataclassList; 
     dataGridView1.DataSource = bindingSource1; 

當運行該應用程序,數據網格是顯示了3列,但在組合框列不會產生。

請幫我找到問題。

在此先感謝。

回答

1

這對我來說是什麼在起作用:

// This is the list of items to be displayed in the DataGridView Combobox Column 
string[] listOfItems = new string[]{"Apple", "Banana", "Orange"}; 

// Define a BindingSource and add the items to it (alas, there is no AddRange()) 
BindingSource bs = new BindingSource(); 

foreach (string item in listOfItems) 
{ 
    bs.Add(item); 
} 

// Set binding (MyComboColumn is the name you gave to your combo column, see image below) 
this.MyComboColumn.DataSource = bs; 

enter image description here

相關問題