I have one dataGrid filled with some values.
I need to copy the contents of dataGrid to dataTable.
After copying the DataTable to Excel sheet, the result was not the expected one. Kinly help in fixing this. The below was the result.
private void btnSave_Click(object sender, RoutedEventArgs e)
{
dt = new System.Data.DataTable();
for (int i = 0; i < dataGrid.Columns.Count; i++)
{
dt.Columns.Add();
}
System.Data.DataRow dr;
for (int row = 0; row < dataGrid.Items.Count; row++)
{
dr = dt.NewRow();
for (int col = 0; col < dt.Columns.Count; col++)
{
dr[col] = dataGrid.Items[col];
}
dt.Rows.Add(dr);
dt.AcceptChanges();
}
//dt = new System.Data.DataTable();
//dt = (System.Data.DataTable)dataGrid.ItemsSource;
}