简体   繁体   中英

Error when binding gridview data to a datatable

I've a gridview in which i bind some data to it.After binding it i want to get the gridview content in a new data table.

Here's my c# code

    Datatable dt_NewTable = new DataTable();        
    foreach (GridViewRow gvRow in gdMainDetails.Rows)
    {            
        DataRow dr = dt_NewTable.NewRow();
        for (int i = 0; i < gvRow.Cells.Count; i++)
        {
            dr[i] = gvRow.Cells[i].Text;
        }
        dt_NewTable.Rows.Add(dr);
    }

The problem is that i'm getting 'gvRow.Cells[i].Text' as "". What's the problem ??

gridView Cell is a container...you must have to pull out the control from it and than get its text..like

Label lblControl = (Label)gvRow.Cells[i].FindControl("controlID"); dr[i] = lblControl .text;

And by the way you also have to define the column(s) of Your DataTable first..!!! ie structure of the Datatable should be same as your grid wants..!!

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