如何将gridView行添加到dataTable中

本文关键字:dataTable 添加 gridView | 更新日期: 2023-09-27 18:21:44

这不起作用:

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

它所做的是将文本"System.Web.UI.WebControls.GridViewRow"放入数据表的第一列。

如何将gridView行添加到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)
             {
                    DataRow dr= finalShowsTable.NewRow();
                     for (int i = 0; i < gvr.Cells.Count - 1; i++)
                     {
                         dr[i] = row.Cells[i].Text;
                     }
                     finalShowsTable.Rows.Add(dr);
             }
         }
    }