简体   繁体   中英

How do I add gridView rows to a dataTable?

This is not working:

var originalFinalShowsTable = Session["finalShowsTable"] as DataTable;

DataTable finalShowsTable = new DataTable();

finalShowsTable = originalFinalShowsTable.Clone();

foreach (GridViewRow gvr in gvShows.Rows)
{
    if (gvr.RowType == DataControlRowType.DataRow)
    {
         if (((CheckBox) gvr.FindControl("cbSelect")).Checked)
         {
               finalShowsTable.Rows.Add(gvr);
         }
     }
}

What it's doing is putting the text "System.Web.UI.WebControls.GridViewRow" into the first column of the data table.

Try this :

DataTable finalShowsTable = new DataTable();

    finalShowsTable = originalFinalShowsTable.Clone();

    foreach (GridViewRow gvr in gvShows.Rows)
    {
        if (gvr.RowType == DataControlRowType.DataRow)
        {
             if (((CheckBox) gvr.FindControl("cbSelect")).Checked)
             {
                    DataRow dr= finalShowsTable.NewRow();
                     for (int i = 0; i < gvr.Cells.Count - 1; i++)
                     {
                         dr[i] = row.Cells[i].Text;
                     }

                     finalShowsTable.Rows.Add(dr);
             }
         }
    }

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