问题描述
- WPF DataGrid转化为DataTable?
-
WPF DataGrid的数据源由ItemsSource绑定,如何将DataGrid转化为DataTable?
补充:
ItemsSource = "{Binding ChargeList}"ChargeList 的类型 :ObservableCollection
解决方案
public static DataTable DataGrid2Table(DataGrid dg)
{
DataTable dt=new DataTable();
for (int i= 0; i< dg.Columns.Count; i++)
{
DataColumn dc = new DataColumn(dg.Columns[i].Name.ToString());
dt.Columns.Add(dc);
}
for (int i = 0; i< dg.Rows.Count; i++)
{
DataRow dr = dt.NewRow();
for (int j = 0; j < dg.Columns.Count; j++)
{
dr[j] = dg.Rows[i].Cells[j].Value.ToString();
}
dt.Rows.Add(dr);
}
return dt;
}
时间: 2024-11-03 20:47:43