Hi,
Refer the below code for converting Gridview To DataTable.
private DataTable GetDataTable(GridView gridView)
{
DataTable dt = new DataTable();
// add the columns to the datatable
if (gridView.HeaderRow != null)
{
for (int i = 0; i < gridView.HeaderRow.Cells.Count; i++)
{
dt.Columns.Add(gridView.HeaderRow.Cells[i].Text);
}
}
// add each of the data rows to the table
foreach (GridViewRow row in gridView.Rows)
{
DataRow dr;
dr = dt.NewRow();
for (int i = 0; i < row.Cells.Count; i++)
{
dr[i] = row.Cells[i].Text.Replace(" ", "");
}
dt.Rows.Add(dr);
}
return dt;
}