2011-07-31 47 views
0

我想在C#中的datagridview1徵收項目組合框DataGrid中C#

說的有3行(Number,Name and Country)

如果我想給一個在comboboxdatagridview1

行最多爲標題或描述的(Number,Name and Country)

下面

是.........選定組合框下拉

組合框數有5個選擇或5徵收項目("1","2","3","4","5")

組合框名稱的有5個選擇或5徵收項目("A","B","C","D","E")

組合框國家有5個選擇或5徵收項目("Amerika","England","Indonesia","Australia","India")

u能幫助我如何使它?

回答

3

每列將需要DataGridViewComboBoxColumn.類型的

此列類型,然後暴露各種屬性,讓你正確設置列組合框的選項。

對於非常簡單的情況下,你所要求的,你可以(在這裏只是說該國列)做這樣的事情:

DataGridViewComboBoxColumn col = new DataGridViewComboBoxColumn(); 
col.HeaderText = "Country"; 
col.Name = "Country"; 
col.Items.AddRange("Amerika","England","Indonesia","Australia","India"); 

dataGridView1.Columns.Add(col); 

現在你在使用這些國家的電網得到的組合框列。

還有很多其他選項可以爲您提供更多的靈活性和強大功能。例如,如果DataGridView具有包含「ChosenCountry」屬性的DataSource,則可以在列和DataSource之間設置綁定。

col.DataPropertyName = "ChosenCountry"; 

一步更多,你可以使用這些國家的ID,而不是依賴於純文本:

List<Country> countries = new List<Country>(); 
countries.Add(new Country { Id = 0, Name = "Amerkia" }); 
countries.Add(new Country { Id = 1, Name = "England" }); 
// and so on or even better get this from some external store using a query 

DataGridViewComboBoxColumn col = new DataGridViewComboBoxColumn(); 

// Now you can use the CheckBoxColumn datasource: 
col.DataSource = countries; 
// And you set the Display and value members and the DataPropertyName: 
col.DisplayMember = "Name"; 
col.ValueMember = "Id"; 
col.DataPropertyName = "CountryId"; 

除了這一點,你需要問詳細說明您確切的情況和問題的問題得到更具體的答案。