I need to add Dictionary<string,List<string>>
contents to a Data Table in C#. The Header column name should be the Key
in Dictionary and the rows should be the List<string>
contents.
How can I get it?
Code:
DataTable new_dt = new DataTable();
//Header Columns
foreach (string item in splits)
{
DataColumn col = new DataColumn(item, typeof(System.String));
new_dt.Columns.Add(col);
}
foreach (DataColumn col in new_dt.Columns)
{
//Declare the bound field and allocate memory for the bound field.
BoundField bfield = new BoundField();
//Initalize the DataField value.
bfield.DataField = col.ColumnName;
//Initialize the HeaderText field value.
bfield.HeaderText = col.ColumnName;
//Add the newly created bound field to the GridView.
gvMatrix.Columns.Add(bfield);
}
//Content Loading
foreach (KeyValuePair<string, List<string>> item in _dict)
{
string[] ss = item.Value.ToArray();
foreach (string s in ss)
{
DataRow row = new_dt.NewRow();
row[item.Key] = s;
new_dt.Rows.Add(row);
}
}
gvMatrix.DataSource = new_dt;
gvMatrix.DataBind();