1

我的 Table1 有一些标题和值:

KundeID KundeName  Produkt   Comment

1       Michael    Jogurt   "nichts"  
2       Raj                 "Ich bin cool"     
3       Gary       Fanta    "yahoo"  
4       Miky       Sprite  

我想更改为 Table2,将 Produkt 中的值设为标题列:

KundeID KundeName Comment        Jogurt Fanta Sprite    

1       Michael   "nichts"         x
2       Raj       "Ich bin cool"           
3       Gary      "yahoo"                 x     
4       Miky                                     x         

我的表 1 代码:

DataTable table = new DataTable("Kunde");

        table.Columns.Add("KundeID", typeof(Int32));
        table.Columns.Add("KundeName", typeof(String));
        table.Columns.Add("Produkt", typeof(String));

        DataTable comment = new DataTable("Comment"); 

        comment.Columns.Add("KundeName", typeof(String));
        comment.Columns.Add("Comment", typeof(String));

        DataSet ds = new DataSet("DataSet");

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

        object[] o1 = { 1, "Michael", "Jogurt" };
        object[] o2 = { 2, "Raj" };
        object[] o3 = { 3, "Gary", "Fanta" };
        object[] o4 = { 4, "Miky", "Sprite" };

        object[] c1 = { "Raj", "Ich bin cool" };
        object[] c2 = { "Gary", "yahoo" };
        object[] c3 = { "Michael", "nichts" };

        table.Rows.Add(o1);
        table.Rows.Add(o2);
        table.Rows.Add(o3);
        table.Rows.Add(o4);

        comment.Rows.Add(c1);
        comment.Rows.Add(c2);
        comment.Rows.Add(c3);

        var results = from table1 in table.AsEnumerable()
                      join table2 in comment.AsEnumerable()
                      on table1.Field<string>("KundeName") equals table2.Field<string>("KundeName") into prodGroup
                      from table4 in prodGroup.DefaultIfEmpty()

                      select new
                          {
                              KundeID = table1.Field<Int32?>("KundeID"),
                              KundeName = table1.Field<String>("KundeName"),
                              Produkt = table1.Field<String>("Produkt"),
                              Comment = table4 != null ? table4.Field<String>("Comment") : null,
                          };
dataGridView1.DataSource = results.ToList();

如何从“Produkt”中获取价值并使其成为标题?谢谢你们的帮助

4

3 回答 3

2

无论出现多少不同的产品,这都应该可以解决问题。它非常简短。

// build the new data table
var result = new DataTable();
result.Columns.Add("KundeID", typeof(Int32));
result.Columns.Add("KundeName", typeof(String));
result.Columns.Add("Comment", typeof(String));
result.Columns.AddRange(
    (from c in 
         (from r in table.AsEnumerable() 
          where !r.IsNull("Produkt") && !string.IsNullOrEmpty(r.Field<string>("Produkt")) 
          select r.Field<string>("Produkt")).Distinct()    // added DISTINCT
     select new DataColumn(c, typeof(bool))).ToArray()
);

foreach (var r in results)
{
    var productIndex = result.Columns.IndexOf(r.Produkt);
    var vals = new List<object>() { r.KundeID, r.KundeName, r.Comment };
    for (int i = 3; i < result.Columns.Count; i++)
    {
        if (i == productIndex)
        {
            vals.Add(true);
        }
        else
        {
            vals.Add(false);
        }
    }

    result.LoadDataRow(vals.ToArray(), true);
}
于 2012-09-26T12:25:06.337 回答
1
var products = table.AsEnumerable()
        .GroupBy(c => c["Produkt"])
        .Where(g => !(g.Key is DBNull))
        .Select(g => (string)g.Key)
        .ToList();

var newtable = table.Copy();
products.ForEach(p=>newtable.Columns.Add(p,typeof(bool)));

foreach (var row in newtable.AsEnumerable())
{
    if (!(row["Produkt"] is DBNull)) row[(string)row["Produkt"]] = true;
}

newtable.Columns.Remove("Produkt");
于 2012-09-26T12:27:00.690 回答
0

尝试关注...这可能会对您有所帮助....

    DataTable table = new DataTable("Kunde"); 
    table.Columns.Add("KundeID", typeof(Int32)); 
    table.Columns.Add("KundeName", typeof(String)); 
    table.Columns.Add("Produkt", typeof(String));
    table.Columns.Add("Comment", typeof(String));

    object[] o1 = { 1, "Michael", "Jogurt", "nichts" }; 
    object[] o2 = { 2, "Raj","","Ich bin cool" };
    object[] o3 = { 3, "Gary", "Fanta","yahoo" }; 
    object[] o4 = { 4, "Miky", "Sprite","" };

    table.Rows.Add(o1); 
    table.Rows.Add(o2); 
    table.Rows.Add(o3); 
    table.Rows.Add(o4); 
    Dictionary<int,string> dictObj=new Dictionary<int, string>();
    for (int i = 0; i < table.Rows.Count; i++)
    {
        dictObj.Add(Convert.ToInt32(table.Rows[i][0].ToString()), table.Rows[i][2].ToString());
    }
    foreach (var obj in dictObj)
    {
        if (string.IsNullOrEmpty(obj.Value))
        {
            table.Columns.Add(obj.Value, typeof(String));
            DataRow row = table.Rows[obj.Key];
            row[obj.Value] = "X";
        }
    }
于 2012-09-26T12:06:32.600 回答