I have some problems with my program, I made join 2 tables and now i want to make a column values as header columns,
1) My tables shows something like this:
ID Name Produkt Comment Attribute 1 Mike AA YY aa 1 Mike AA YY cc 2 Steve BB YY aa 3 None XX cc 4 Hube DD
2) I want my tables be like this:
ID Name Produkt Comment aa cc 1 Mike AA YY x x 2 Steve BB YY x 3 None XX x 4 Hube DD
My Code for 1)
DataTable table = new DataTable("table");
table.Columns.Add("ID", typeof(Int32));
table.Columns.Add("Name", typeof(String));
table.Columns.Add("Produkt", typeof(String));
table.Columns.Add("Attribute", 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","aa" };
object[] o5 = { 1, "Mike", "AA", "cc" };
object[] o2 = { 2,"Steve","BB","aa" };
object[] o3 = { 3,"None",null , "cc" };
object[] o4 = { 4,"Hube", "DD", };
object[] c1 = { "None","XX" };
object[] c2 = { "Steve","YY"};
object[] c3 = { "Mike", "YY"};
table.Rows.Add(o1);
table.Rows.Add(o5);
table.Rows.Add(o2);
table.Rows.Add(o3);
table.Rows.Add(o4);
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"),
Attribute = t1.Field<String>("Attribute"),
Comment = table4 != null ? table4.Field<String>("Comment") : null,
};
dataGridView2.DataSource = results.ToList();
This post has been edited by Curtis Rutland: 01 October 2012 - 07:17 AM
Reason for edit:: fixed code tags

New Topic/Question
Reply



MultiQuote



|