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

上一篇:[转]Delphi 中 image 控件加载bmp、JPG、GIF、PNG等图片的办法


下一篇:c#桥接模式(bridge结构模式)