Raja

Raja

  • NA
  • 2k
  • 357k

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.
 
 sample1
 
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++)
 {
dt
.Columns.Add("column"+i.ToString());
}
foreach (GridViewRow row in GridView1.Rows)
 {
 DataRow dr = dt.NewRow();
 for(int j = 0;j<GridView1.Columns.Count;j++)
 {
dr
["column" + j.ToString()] = row.Cells[j].Text;
 } dt.Rows.Add(dr);
 }

 

Answers (3)