I am designing a winform which have a datagridview.
I am assigning a datatable to that datagridview.
DataTable dt = new DataTable();
dt.Columns.Add("items", typeof(string[]));
dt.Columns.Add("dateSold", typeof(string));
for (int i = 0; i < 6; i++)
{
dt.Rows.Add(dt.NewRow());
dt.Rows[i]["items"] = new string[] { "pencil" + i, "sharpner" + i };
dt.Rows[i]["dateSold"] = "0" + i + "/0" + i + "/0" + i;
}
dataGridView1.DataSource = dt;
Column items is an array of strings. I want to show this array in one coloum of the grid.
But by simply assigning the datatable as the datasource of the datagridview doesn’t help.
The syntax you’re using is a bit strange. There are overloads that would allow you to add a new row and set a value at the same time.
Also, you can’t have a string array be a field for a cell. Instead of using a DataTable you could create a custom class, and bind a list of them to the DataGridView. This way you could have an underlying field which is an array of strings, and then have a public property that formats them as you desire, and then displays that in the DataGridView.
Here’s a sample of how to approach this: