//ObservableCollection 表示一个动态数据集合,它可在添加、删除项目或刷新整个列表时提供通知。
//ExpandoObject 表示可在运行时动态添加和删除其成员的对象
ObservableCollection<ExpandoObject> listobject = new ObservableCollection<ExpandoObject>();
foreach (DataRow dr in datatable.Rows)
{
dynamic d = new System.Dynamic.ExpandoObject();
foreach (DataColumn dc in datatable.Columns)
{
(d as ICollection<KeyValuePair<string, object>>).Add(new KeyValuePair<string, object>(dc.ColumnName, dr[dc]));
}
listobject.Add(d);
}
dataGridTable.ItemsSource = listobject;//绑定到DataGrid