c#将datagridview转datatable
时间:2011-04-11 来源:Jerry's Paradise
using System;
using System.Collections.Generic;
using System.Data;
using System.Windows.Forms;
using System.Linq;
using System.Text;
namespace Test
{
public class DataTransition
{
/// <summary>
/// datagridview转datatable
/// </summary>
/// <param name="dv"></param>
/// <returns></returns>
public static DataTable dvtodt(DataGridView dv)
{
DataTable dt = new DataTable();
DataColumn dc;
for (int i = 0; i < dv.Columns.Count; i++)
{
dc = new DataColumn();
dc.ColumnName = dv.Columns[i].HeaderText.ToString();
dt.Columns.Add(dc);
}
for (int j = 0; j < dv.Rows.Count; j++)
{
DataRow dr = dt.NewRow();
for (int x = 0; x < dv.Columns.Count; x++)
{
dr[x] = dv.Rows[j].Cells[x].Value;
}
dt.Rows.Add(dr);
}
return dt;
}
}
}