简体   繁体   English

如何将gridView行添加到dataTable?

[英]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. 它正在做的是将文本"System.Web.UI.WebControls.GridViewRow"放入数据表的第一列。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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