Hi
I have below Gridview. How to convert it into datatable.
<%--DataKeyNames="BookID" OnRowEditing="OnRowEditing" OnRowCancelingEdit="OnRowCancelingEdit" OnRowUpdating="OnRowUpdating"--%>
Thanks
Naimish MakwanaPosted May 1, 2023, 4:52 AM
Try this:
Thanks
Amit MohantyPosted May 1, 2023, 4:37 AM
To convert the GridView data into a DataTable, you can use the following code:
Aman GuptaPosted May 1, 2023, 3:08 AM
Hello,
You should refer this link, here we have provided the solution in different programming languages.
https://www.codingvila.com/2019/03/convert-gridview-to-datatable-in-c-sharp-and-vb.html?m=1
Vishal YelvePosted Apr 30, 2023, 8:07 AM
Try this
DataTable GetDataTable(GridView dtg)
{
DataTable dt = new DataTable();
// add the columns to the datatable
if (dtg.HeaderRow != null)
{
for (int i = 0; i < dtg.HeaderRow.Cells.Count; i++)
{
dt.Columns.Add(dtg.HeaderRow.Cells[i].Text);
}
}
// add each of the data rows to the table
foreach (GridViewRow row in dtg.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;
}