将dataGridView数据转成DataTable

简介: 如已绑定过数据源: DataTable dt = (dataGridView1.DataSource as DataTable) 如未绑定过数据源:public DataTable GetDgvToTable(DataGridView dgv)     ...

如已绑定过数据源:

DataTable dt = (dataGridView1.DataSource as DataTable)

如未绑定过数据源:
public DataTable GetDgvToTable(DataGridView dgv)
        {
            DataTable dt = new DataTable();

            // 列强制转换
            for (int count = 0; count
            {
                DataColumn dc = new DataColumn(dgv.Columns[count].Name.ToString());
                dt.Columns.Add(dc);
            }

            // 循环行
            for (int count = 0; count
            {
                DataRow dr = dt.NewRow();
                for (int countsub = 0; countsub
                {
                    dr[countsub] = Convert.ToString(dgv.Rows[count].Cells[countsub].Value);
                }
                dt.Rows.Add(dr);
            }
            return dt;
        }

转自网址:

http://www.cnblogs.com/linji/archive/2012/10/11/2719471.html

相关文章
C#读取Excel表格中数据并返回datatable
在软件开发的过程中,经常用到从excel表格中读取数据作为数据源,以下整理了一个有效的读取excel表格的方法。   DataTable GetDataTable(string tableName,string leftTopCel,string rightbutCel) ...
1441 0