0

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.Here is 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;
    }

1 Answers1

0

instead of reading from data grid read it from the bound collection

var table = new DataTable();
using(var reader = ObjectReader.Create(data)) 
{
    table.Load(reader);
}

and there you have a data table, for further details you can look here

Muds
  • 4,006
  • 5
  • 31
  • 53