,我有以下的數據我想在一個DataGridView顯示:綁定不同的條目的DataGridViewComboBoxCell
DataEntry[] data = new[]{
new DataEntry(){Name = "A", Entries = new []{ "1", "2"}},
new DataEntry(){Name = "B", Entries = new []{ "1", "2", "3"}}};
「名稱」將是一個簡單的文本框字段,「項」組合框,其中可供選擇的項目是列表中的元素。
因此,在這個例子中會有2行(下面是在DataGridView會是什麼樣子):
Name Entries
Row1 : A <choice of "1" or "2">
Row1 : B <choice of "1" or "2" or "3">
我的問題是,我怎麼綁定這個數據?我已經看了DataPropertyName,DisplayMember和ValueMember屬性......但是不能完成這一項。
下面是代碼 - 它帶有一個註釋,我需要添加一些神奇的行來爲Entries列設置DataSource等。
public partial class Form1 : Form
{
DataEntry[] data = new[]{
new DataEntry(){Name = "A", Entries = new []{ "1", "2"}},
new DataEntry(){Name = "B", Entries = new []{ "1", "2", "3"}}};
public Form1()
{
InitializeComponent();
dataGridView1.AutoGenerateColumns = false;
var nameCol = new DataGridViewTextBoxColumn();
nameCol.DataPropertyName = "Name";
var entriesCol = new DataGridViewComboBoxColumn();
//entriesCol. ???? = "Entries"; !!
dataGridView1.Columns.AddRange(new DataGridViewColumn[] { nameCol, entriesCol });
dataGridView1.DataSource = data;
}
}
public class DataEntry
{
public string Name { get; set; }
public IEnumerable<string> Entries { get; set; }
}