DataGridView 转换成 DataTable
1 public static DataTable ConvertDgvDataToTable(DataGridView dgv) 2 { 3 DataTable dt = new DataTable(); 4 for (int count = 0; count < dgv.Columns.Count; count++) //循环DataGridView中的栏位 5 { 6 DataColumn dc = new DataColumn(dgv.Columns[count].Name.ToString()); 7 dt.Columns.Add(dc); //将DataGridView中栏位的标题写入到DataTable中 8 } 9 for (int count = 0; count < dgv.Rows.Count; count++) //循环DataGridView中的资料行 10 { 11 DataRow dr = dt.NewRow(); 12 for (int countsub = 0; countsub < dgv.Columns.Count; countsub++) 13 { 14 dr[countsub] = dgv.Rows[count].Cells[countsub].Value.ToString(); 15 } 16 dt.Rows.Add(dr); 17 } 18 return dt; 19 }