首页 > 代码库 > .Net常用技巧_将DataGridView的内容转换成DataTable

.Net常用技巧_将DataGridView的内容转换成DataTable

using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Text;using System.Windows.Forms;using System.Data.SqlClient;using Utility;namespace MyTool{    public class GetDgvToTable    {        public static DataTable GetTable(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;        }    }}