将DataGridView转换为DataTable
将DataGridView转换为DataTable
如已绑定过数据源:DataTable dt = (dataGridView1.DataSource DataTable)
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;
}
原文地址:https://blog.csdn.net/baidu_27474941/article/details/52965412
越努力,越幸运!