How to add gridview rows to a datatable?

前端 未结 1 1521
被撕碎了的回忆
被撕碎了的回忆 2020-12-19 15:48

I have a gridview which will contain some \'n\' number of rows.... Now i want to add all rows of the gridview to a datatable which will be used for bulkcopy operation...

相关标签:
1条回答
  • 2020-12-19 16:19

    you can traverse datagrid row by row and make a comma separated file. then use Bulk insert or bcp for inserting data to db.

    Another Solution

        DataTable dt = new DataTable();    
        for (int j = 0; j < grdList.Rows.Count; j++)
        {
            DataRow dr;
            GridViewRow row = grdList.Rows[j];
            dr = dt.NewRow();
            for (int i = 0; i < row.Cells.Count; i++)
            {
                dr[i] = row.Cells[i].Text;
            }
    
            dt.Rows.Add(dr);
        }
    
    SqlBulkCopy sbc = new SqlBulkCopy(targetConnStr);
    sbc.DestinationTableName = "yourDestinationTable";
    sbc.WriteToServer(dt);
    sbc.Close();
    
    0 讨论(0)
提交回复
热议问题