I have the following data I want to display in a DataGridView:
DataEntry[] data = new[]{
new DataEntry(){Name = "A", Entries = new []{ "1", "2"}},
new DataEntry(){Name = "B", Entries = new []{ "1", "2", "3"}}};
"Name" will be a simple TextBox field, and "Entries" a ComboBox where the items to choose from are the elements in the list.
So in this example there would be 2 rows (below is what the datagridview would look like):
Name Entries
Row1 : A <choice of "1" or "2">
Row1 : B <choice of "1" or "2" or "3">
My question is, how do I bind this data?! I've looked at the DataPropertyName, DisplayMember and ValueMember properties... but just cannot work this one out.
Below is the code as it stands - with a comment where I need to add the magic couple of lines to set the DataSource etc. for the Entries column.
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; }
}