DataTable dt = new DataTable();
for (int i = dt.Rows.Count; i < dataGridView1.Rows.Count; i++)
{
DataRow dr = dt.NewRow();
for (int j = 0; j < dataGridView1.Columns.Count; j++)
{
dr[dataGridView1.Columns[j].Name] = dataGridView1.Rows[i].Cells[j].Value;
}
dt.Rows.Add(dr);
}
for (int i = dt.Rows.Count; i < dataGridView1.Rows.Count; i++)
{
DataRow dr = dt.NewRow();
for (int j = 0; j < dataGridView1.Columns.Count; j++)
{
dr[dataGridView1.Columns[j].Name] = dataGridView1.Rows[i].Cells[j].Value;
}
dt.Rows.Add(dr);
}