Hi
I have below gridview . I want to convert it into DataTable
Code
Thanks
Hi
I have below gridview . I want to convert it into DataTable
Thanks
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Rajkiran SwainRank #165 · 14.4K reputationPosted May 22, 2023, 11:42 AM EDT
// Create a new DataTable DataTable dataTable = new DataTable(); // Add columns to the DataTable based on the GridView's header foreach (DataControlFieldHeaderCell headerCell in grdPlanning.HeaderRow.Cells) { dataTable.Columns.Add(headerCell.Text); } // Iterate through the GridView's rows and populate the DataTable foreach (GridViewRow row in grdPlanning.Rows) { DataRow dataRow = dataTable.NewRow(); for (int i = 0; i < row.Cells.Count; i++) { dataRow[i] = row.Cells[i].Text; } dataTable.Rows.Add(dataRow); } // The 'dataTable' variable now contains the converted data from the GridViewMudzakkir TohaRank #3,303 · 435 reputationPosted May 22, 2023, 2:12 PM EDT
Hi,
I think your idea is not best practice of using datasource.
I think better you make private List
Converting gridView to dataTable will causing possibility of conversion error, and take efforts, when binding you just need to update value listDatasource from DB, and passing listDatasource object to gridview.
Naimish MakwanaRank #324 · 7.7K reputationPosted May 22, 2023, 11:50 PM EDT
Try below:
using System; using System.Data; using System.Web.UI.WebControls; // Assuming this code is in the code-behind file of your ASP.NET page or user control protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { // Bind the GridView to its data source // ... // Call the method to convert GridView to DataTable DataTable dataTable = ConvertGridViewToDataTable(grdPlanning.DataSource); // Use the resulting DataTable as needed // ... } } private DataTable ConvertGridViewToDataTable(object dataSource) { DataTable dataTable = new DataTable(); // Retrieve the data source and cast it to a DataView DataView dataView = dataSource as DataView; if (dataView != null) { // Create columns in DataTable based on the DataView's schema foreach (DataColumn column in dataView.Table.Columns) { dataTable.Columns.Add(column.ColumnName, column.DataType); } // Iterate through each row in the DataView and populate the DataTable foreach (DataRowView dataRowView in dataView) { DataRow dataRow = dataTable.NewRow(); for (int i = 0; i < dataRowView.Row.ItemArray.Length; i++) { dataRow[i] = dataRowView.Row.ItemArray[i]; } dataTable.Rows.Add(dataRow); } } return dataTable; }Thanks
Jignesh KumarRank #91 · 22.9K reputationPosted May 23, 2023, 12:49 AM EDT
Hello Ramco,
using System; using System.Data; using System.Web.UI.WebControls; public DataTable ConvertGridViewToDataTable(GridView gridView) { DataTable dataTable = new DataTable(); foreach (DataControlField column in gridView.Columns) { dataTable.Columns.Add(column.HeaderText); } // Iterate through each row in the GridView foreach (GridViewRow row in gridView.Rows) { DataRow dataRow = dataTable.NewRow(); // Iterate through each cell in the GridView row for (int i = 0; i < row.Cells.Count; i++) { dataRow[i] = row.Cells[i].Text; } dataTable.Rows.Add(dataRow); } return dataTable; }You can call,
GridView myGridView = new GridView(); DataTable myDataTable = ConvertGridViewToDataTable(myGridView);Vishal YelveRank #178 · 13.6K reputationPosted May 23, 2023, 1:49 AM EDT
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; }Vishal YelveRank #178 · 13.6K reputationPosted May 24, 2023, 1:08 AM EDT
Hi Ramco,
https://www.aspsnippets.com/Articles/Using-jQuery-DataTables-with-GridView-in-ASPNet.aspx
Ramco RamcoRank #462 · 5.4K reputationPosted May 23, 2023, 11:17 AM EDT
Hi all
I want to convert Gridview to JQuery Datatable.
Thanks