Uniek Le

Uniek Le

  • NA
  • 2
  • 5.5k

Inverse Datatable and view in Datagridview

Oct 9 2012 9:53 AM
I have a table and want to inverse it and shows it in Datagridview.
My Table looks like that:
 ID Name Produkt Comment
 1 Mike AA YY
 1 Steve BB YY
 1 Lola CC
 3 None
 XX

I want it looks like that:
 1 3
 Mike Steve Lola None
 AA BB CC
 YY YY
 XX

My code for the table:

  DataTable table = new DataTable("table");
  table.Columns.Add("ID", typeof(Int32));
  table.Columns.Add("Name", typeof(String));
  table.Columns.Add("Produkt", typeof(String));

  DataTable table2 = new DataTable("table2");
  table2.Columns.Add("Name", typeof(String));
  table2.Columns.Add("Comment", typeof(String));
  DataSet ds = new DataSet("DataSet");

  ds.Tables.Add(table);
  ds.Tables.Add(table2);

  object[] o1 = { 1, "Mike", "AA"};
  object[] o2 = { 1,"Steve","BB"};
  object[] o4 = { 1, "Lola", "CC" };
  object[] o3 = { 3,"None",null  };
  object[] c1 = { "None","XX" };
  object[] c2 = { "Steve","YY"};
  object[] c3 = { "Mike", "YY"};

  table.Rows.Add(o1);
  table.Rows.Add(o2);
  table.Rows.Add(o4);
  table.Rows.Add(o3);
  table2.Rows.Add(c1);
  table2.Rows.Add(c2);
  table2.Rows.Add(c3);
 
  var results = from t1 in table.AsEnumerable()
  join tb2 in table2.AsEnumerable()
  on t1.Field<string>("Name") equals tb2.Field<string>("Name") into prodGroup
  from table4 in prodGroup.DefaultIfEmpty()
  select new
  {
  ID = t1.Field<Int32?>("ID"),
  Name = t1.Field<String>("Name"),
  Produkt = t1.Field<String>("Produkt"),
  Comment = table4 != null ? table4.Field<String>("Comment") : null,
  } ;
  dataGridView2.DataSource = results.ToList();

Can someone give me a key or show me how to do it? I'm very thankfull