• 774
  • 1.9k
  • 199.6k

How to convert Gridview to datatable in asp.net c#?

May 9 2017 3:32 AM
i have upload one excel file and convert into datatable and bind the datatable in gridview.
i have one button click to add new row in grid view. i want get old data into datatable and adding new row how to do it.
i have used below codes to get grid view to data table but this is not working.that codes add only new row old rows are remove or hided.
DataTable gridTable = (DataTable) dataGrid1.DataSource; 
sample 2
BindingSource bs = (BindingSource)dgrid.DataSource; // Se convierte el DataSource 
DataTable tCxC = (DataTable) bs.DataSource;

sample 3

DataTable dt = new DataTable();
for (int i = 0; i < GridView1.Columns.Count; i++)
foreach (GridViewRow row in GridView1.Rows)
 DataRow dr = dt.NewRow();
 for(int j = 0;j<GridView1.Columns.Count;j++)
["column" + j.ToString()] = row.Cells[j].Text;
 } dt.Rows.Add(dr);


Brought to you by: JavaScript SDK for Bold BI dashboard and analytics embedding. Free trial.

Answers (3)