public DataTable GetDgToTable(DataGrid dgv)
{
DataTable dt = new DataTable()
for (int count = 0count <dgv.Columns.Countcount++)
{
DataColumn dc = new DataColumn(dgv.Columns[count].Name.ToString())
dt.Columns.Add(dc)
}
for (int count = 0count <dgv.Rows.Countcount++)
{
DataRow dr = dt.NewRow()
for (int countsub = 0countsub <dgv.Columns.Countcountsub++)
{
dr[countsub] = Convert.ToString(dgv.Rows[count].Cells[countsub].Value)
}
dt.Rows.Add(dr)
}
return dt
}
如果你设定AutoGenerateColumns就不会自动生成列,需要手动指定datagrid的列。例如:
<DataGrid AutoGenerateColumns="False" Height="418" HorizontalAlignment="Left" Name="dataGrid1" VerticalAlignment="Top" Width="316" >
<DataGrid.Columns>
<DataGridTextColumn Header="列名" Binding="{Binding Path=你的ds.Tables[0]的某一列的列名, Mode=TwoWay}" Width="380"/>
</DataGrid.Columns>
</DataGrid>
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)