Вам необходимо сначала создать тип DataRow и добавить его в таблицу DataTable, прежде чем вы сможете назначить его.
Так что ваш код теперь будет выглядеть примерно так:
DataSet ds = new DataSet();
DataTable dt = new DataTable("ProdFromDGV");
ds.Tables.Add(dt);
foreach (DataGridViewColumn col in dataGridView1.Columns)
{
dt.Columns.Add(col.HeaderText, typeof(string));
}
foreach (DataGridViewRow row in dataGridView1.Rows)
{
foreach (DataGridViewCell cell in row.Cells)
{
// This will not work, but it's something similar to this that you need here...
DataRow row = new DataRow();
dt.RowCollecion.Add(row);
// Now you can assign to the row....
dt.Rows[row.Index][cell.ColumnIndex] = cell.Value.ToString();
}
}
dt.WriteXml("table.xml");
Надеюсь, это поможет некоторым ..