如何将一个数据表的DataRow值更改为新数据表的列名?

时间:2016-12-22 11:10:07

标签: c# datatable

我的第一个DataTable是

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

我希望这个表格为

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

如何解决这个问题,请帮助我,我尝试了很多,但我没有得到。谢谢你提前。

2 个答案:

答案 0 :(得分: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;
}

如果您有任何问题或疑问,请随时问我。

答案 1 :(得分: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");
                }
            }

        }


    }


}