0

I've found this tutorial about binding a list to a DataGridView which is quite nice. Let's consider the example from the tutorial:

enter image description here

I'd like to be able to put the brand (Ford/Sheldy AC/Chevrolet) in the row header automatically. How can I specify it? I've done a few try without any interesting result...

Thomas Ayoub
  • 29,063
  • 15
  • 95
  • 142
  • You need to dynamically create the columns, will work on giving an example – Dave Feb 20 '15 at 10:22
  • see here: http://stackoverflow.com/questions/710064/adding-text-to-datagridview-row-header – Davide Piras Feb 20 '15 at 10:29
  • @DavidePiras I don't see anything about rows header **provided by** the binding list in here. Maybe the way my question was asked was not the best or I missed something in the list you provide... But to me, this is not a dup – Thomas Ayoub Feb 20 '15 at 10:32
  • @Thomas so in your case to manually change the property: HeaderCell.Value does not help? – Davide Piras Feb 20 '15 at 11:13
  • @DavidePiras if I do that I'll have the brand name displayed two times, isn't it ? – Thomas Ayoub Feb 20 '15 at 11:21

1 Answers1

1

I am currently re installing everything on my pc so this is the best I could come up with:

private DataTable ConvertListToDataTable(List<List<object>> list)
{
    DataTable table = new DataTable();
    for (int i = 0; i < MyList.Count; i++)
    {
        table.Columns.Add(MyList[i], typeof(string));
    }
    table.Columns.Add("", typeof(object));
    // Add rows data
    List<object[]> tObj = new List<object[]>();
    for (int i = 0; i < list.Count; i++)
    {
        tObj.Add((object[])(list[i].ToArray()));
    }
    for (int i = 0; i < list.Count; i++)
    {
        table.Rows.Add(tObj[i]);
    }
    return table;
}

I hope it helps

Dave
  • 967
  • 10
  • 23