using System.Collections.Generic;
using System.ComponentModel;
using System.Linq.Expressions;
using System.Threading.Tasks;
using System.Windows.Forms;
public partial class Form1 : Form
private void Form1_Load(object sender, EventArgs e)
new Person(){ Name="张三", Age=20, Gender=true, Address="中山路1号"},
new Person(){ Name="李四", Age=32, Gender=true, Address="黄埔路1号"},
new Person(){ Name="王五", Age=25, Gender=true, Address="天河路1号"},
new Person(){ Name="赵六", Age=28, Gender=true, Address="荔湾路1号"},
dataGridView1.DataSource = CustomDataTable(people, p => p.Name, p => p.Age, p => p.Address);
private DataTable CustomDataTable<T>(List datas, params Expressionobject>>[] columns)
DataTable dt = new DataTable();
string[] colName = new string[columns.Length];
foreach (var col in columns)
if (col.Body is UnaryExpression ue && ue.NodeType == ExpressionType.Convert)
prop = ue.Operand as MemberExpression;
prop = col.Body as MemberExpression;
colName[cnt++] = prop.Member.Name;
string attrVal = (prop.Member.GetCustomAttributes(typeof(DescriptionAttribute),false)[0] as DescriptionAttribute).Description;
dt.Columns.Add(new DataColumn(attrVal));
foreach (var item in datas)
for (int i = 0; i < colName.Length; i++)
row[i] = item.GetType().GetProperty(colName[i]).GetValue(item).ToString();
public string Name { get; set; }
public double Age { get; set; }
public bool Gender { get; set; }
public string Address { get; set; }