using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Dynamic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Script.Serialization;
using System.Windows.Forms;
using Newtonsoft.Json;
namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
private List<Test> l = new List<Test>();
public Form1()
{
InitializeComponent();
}
private void button3_Click(object sender, EventArgs e)
{
List<string> rowname = new List<string>();
List<string> colname = new List<string>();
List<IDictionary<String, Object>> l1 = new List<IDictionary<String, Object>>();
//获得行名称
; i < l.Count; i++)
{
bool IsExist = true;
; j < rowname.Count; j++)
{
if (rowname[j].ToString() == l[i].Name)
{
IsExist = false;
break;
}
}
if (IsExist)
rowname.Add(l[i].Name);
}
// 获得列名称
; i < l.Count; i++)
{
bool IsExist = true;
; j < colname.Count; j++)
{
if (colname[j].ToString() == l[i].Type.ToString())
{
IsExist = false;
break;
}
}
if (IsExist)
colname.Add(l[i].Type.ToString());
}
colname.Insert(,"name");
//数据转换
; i < rowname.Count; i++)
{
dynamic exobj = new ExpandoObject();
var obj = (IDictionary<String, Object>) exobj;
obj.Add("name", rowname[i]);
List<Test> t = l.Where(c => c.Name.Equals(rowname[i])).ToList();
; j < t.Count(); j++)
{
obj.Add(t[j].Type.ToString(), t[j].Money);
}
l1.Add(obj);
}
DataTable dt = new DataTable("dt");
//创建列
; i < colname.Count; i++)
{
dt.Columns.Add(colname[i]);
}
//创建行
; i < l1.Count; i++)
{
DataRow r = dt.NewRow();
; j < dt.Columns.Count; j++)
{
r[j] = l1[i][colname[j]];
}
dt.Rows.Add(r);
}
dataGridView2.DataSource = dt;
]);
MessageBox.Show(json1);
}
private void Form1_Load(object sender, EventArgs e)
{
; i < ; i++)
{
Test t = new Test();
t.Type = ;
t.Name = "nametype" + i.ToString();
t.Money = + i;
l.Add(t);
}
; i < ; i++)
{
Test t = new Test();
t.Type = ;
t.Name = "nametype" + i.ToString();
t.Money = + i;
l.Add(t);
}
; i < ; i++)
{
Test t = new Test();
t.Type = ;
t.Name = "nametype" + i.ToString();
t.Money = + i;
l.Add(t);
}
dataGridView1.DataSource = l;
}
public class Test
{
public string Name { get; set; }
public int Type { get; set; }
public decimal Money { get; set; }
}
}
}