首页 > 代码库 > C# DataGridView转DataTable

C# DataGridView转DataTable

        public static DataTable ToDataTable(this DataGridView dataGridView, string tableName = null)        {            DataGridView dgv = dataGridView;            DataTable table = new DataTable(tableName);            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;        }

 

C# DataGridView转DataTable