2016-12-22 2 views
1

私の最初のDataTableがDataTableのDataRow値を新しいDatatableの列名に変更するにはどうすればよいですか?

Name  | Value 
---------------+---------- 
    A   | 12 
    B   | 22 

で、私はこれを解決する方法

 A   |  B 
---------------+---------- 
    12   | 22 

としてこのテーブルをしたい、私は多くのことを試みたが、私は事前にあなたをget.Thankなかった私を助けてください。

+0

あるC#のデータテーブルまたはjQueryのデータテーブルの 'dataTable'タグですか? –

+0

どのデータベース? – JerryGoyal

+1

あなたは[テーブルをピボットする]ように聞こえます。http://www.codingeverything.com/2014/06/PivotDataTable.html – stuartd

答えて

1

あなたは、コードの行の下に使用して列に行を変換することができます:あなたはすべての問題またはクエリを持っている場合は

DataTable Pivot(DataTable table, string pivotColumnName) 
{ 
    // TODO make sure the table contains at least two columns 

    // get the data type of the first value column 
    var dataType = table.Columns[1].DataType; 

    // create a pivoted table, and add the first column 
    var pivotedTable = new DataTable(); 
    pivotedTable.Columns.Add("Row Name", typeof(string)); 

    // determine the names of the remaining columns of the pivoted table 
    var additionalColumnNames = table.AsEnumerable().Select(x => x[pivotColumnName].ToString()); 

    // add the remaining columns to the pivoted table 
    foreach (var columnName in additionalColumnNames) 
    pivotedTable.Columns.Add(columnName, dataType); 

    // determine the row names for the pivoted table 
    var rowNames = table.Columns.Cast<DataColumn>().Select(x => x.ColumnName).Where(x => x != pivotColumnName); 

    // fill in the pivoted data 
    foreach (var rowName in rowNames) 
    { 
     // get the value data from the appropriate column of the input table 
     var pivotedData = table.AsEnumerable().Select(x => x[rowName]); 

     // make the rowName the first value 
     var data = new object[] { rowName }.Concat(pivotedData).ToArray(); 

     // add the row 
     pivotedTable.Rows.Add(data); 
    } 
    return pivotedTable; 
} 

を私にお気軽にお尋ねください。ここ

+0

@ shafi7468あなたが役に立ったと思ったら、それを回答としてマークしてください。そうすれば他の人にも役立ちます。 –

+0

Thanqそれほど多くの作業ファイン。 – shafi7468

1

ピボットテーブル

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Data; 


namespace ConsoleApplication33 
{ 

    class Program 
    { 

     static void Main(string[] args) 
     { 
      DataTable dt = new DataTable(); 
      dt.Columns.Add("Name", typeof(string)); 
      dt.Columns.Add("Value", typeof(int)); 
      dt.Columns.Add("Date", typeof(DateTime)); 

      dt.Rows.Add(new object[] {"A", 100, DateTime.Parse("12/1/16")}); 
      dt.Rows.Add(new object[] { "A", 101, DateTime.Parse("12/2/16") }); 
      dt.Rows.Add(new object[] { "A", 102, DateTime.Parse("12/3/16") }); 
      dt.Rows.Add(new object[] { "A", 103, DateTime.Parse("12/4/16") }); 
      dt.Rows.Add(new object[] { "B", 104, DateTime.Parse("12/1/16") }); 
      dt.Rows.Add(new object[] { "B", 110, DateTime.Parse("12/2/16") }); 
      dt.Rows.Add(new object[] { "B", 114, DateTime.Parse("12/3/16") }); 
      dt.Rows.Add(new object[] { "B", 112, DateTime.Parse("12/4/16") }); 
      dt.Rows.Add(new object[] { "B", 100, DateTime.Parse("12/5/16") }); 
      dt.Rows.Add(new object[] { "C", 120, DateTime.Parse("12/1/16") }); 
      dt.Rows.Add(new object[] { "C", 130, DateTime.Parse("12/2/16") }); 
      dt.Rows.Add(new object[] { "C", 140, DateTime.Parse("12/3/16") }); 
      dt.Rows.Add(new object[] { "C", 150, DateTime.Parse("12/4/16") }); 
      dt.Rows.Add(new object[] { "C", 160, DateTime.Parse("12/5/16") }); 
      dt.Rows.Add(new object[] { "C", 101, DateTime.Parse("12/6/16") }); 

      string[] uniqueNames = dt.AsEnumerable().Select(x => x.Field<string>("Name")).Distinct().ToArray(); 
      var groups = dt.AsEnumerable().GroupBy(x => x.Field<DateTime>("Date")).ToList(); 

      DataTable pivot = new DataTable(); 
      pivot.Columns.Add("Date", typeof(DateTime)); 

      foreach (var name in uniqueNames) 
      { 
       pivot.Columns.Add(name, typeof(string)); 
      } 

      foreach (var group in groups) 
      { 
       DataRow newRow = pivot.Rows.Add(); 
       newRow["Date"] = group.Key; 

       foreach (DataRow row in group) 
       { 
        newRow[row.Field<string>("Name")] = row.Field<int>("Value"); 
       } 
      } 

     } 


    } 


} 
関連する問題