How to add gridview rows to a datatable?

℡╲_俬逩灬. 提交于 2019-11-29 11:28:01

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();
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!