如DataTable已绑定过数据源:
DataTable dt = (dataGridView1.DataSource as DataTable)
remark:使用上面的方法时,把DataGridView的数据赋给没绑定数据的DataTabe,之后改变DataTable时貌似会改变复制的DataGridView的数据
如DataTable未绑定过数据源:
/// <summary> /// 绑定DataGridView数据到DataTable /// </summary> /// <param name="dgv">复制数据的DataGridView</param> /// <returns>返回的绑定数据后的DataTable</returns> public DataTable GetDgvToTable(DataGridView dgv) { DataTable dt = new DataTable(); // 列强制转换 for (int count = 0; count < dgv.Columns.Count; count++) { DataColumn dc = new DataColumn(dgv.Columns[count].Name.ToString()); dt.Columns.Add(dc); } // 循环行 for (int count = 0; count < dgv.Rows.Count; count++) { DataRow dr = dt.NewRow(); for (int countsub = 0; countsub < dgv.Columns.Count; countsub++) { dr[countsub] = Convert.ToString(dgv.Rows[count].Cells[countsub].Value); } dt.Rows.Add(dr); } return dt; }
参考:http://www.cnblogs.com/linji/archive/2012/10/11/2719471.html