/// <summary>
/// 将DataGridView转为DataTable
/// </summary>
/// <param name="Dgv">DataGridView控件名</param>
/// <param name="dt">DataTable表(数据库结构)</param>
/// <returns>DataTable</returns>
public static DTable GetDgvToTable(DataGridView dgv)
{
DTable table = new DTable();
for (int iCol = 0; iCol < dgv.Columns.Count; iCol++)
{
table.Columns.Add(dgv.Columns[iCol].Name);
}
foreach (DataGridViewRow row in dgv.Rows)
{
DataRow datarw = table.NewRow();
for (int iCol = 0; iCol < dgv.Columns.Count; iCol++)
{
datarw[iCol] = row.Cells[iCol].Value;
}
table.Rows.Add(datarw);
}
return table;
}
/// <summary>
/// 将DataTable转为DataGridView
/// </summary>
/// <param name="Dgv">DataGridView控件名</param>
/// <param name="dt">DataTable表(数据库结构)</param>
/// <returns>DataGridView</returns>
public static void GetTableToDgv(DataGridView dgv, DTable table)
{
dgv.Rows.Clear();
foreach (DataRow row in table.Rows) dgv.Rows.Add(row.ItemArray);
}