简体   繁体   中英

Convert Grid view to Data Table

I want to convert gridview data to data table. But my gridview has a lot of data and pageing is true on it. I should write this code (in this code first I load all data without paging):

 Cgv.PageIndex = 0;
 Cgv.AllowPaging = false;
 Cgv.DataBind();

then

 for (int num = 0; num < Cgv.Rows.Count; num++)
    {
        dr = dt.NewRow();
        ....//generate data table
  }

My data is lot and Cgv.DataBind() is take time and at the end I get error.

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);
    }

please try this code

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM