将dataGridView数据转成DataTable

2021-03-09 07:30

阅读:703

标签:数据源   循环   datarow   cells   col   ble   sub   tar   val   

已绑定过数据源:DataTable dt = (dataGridView1.DataSource 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;
        }

 

将dataGridView数据转成DataTable

标签:数据源   循环   datarow   cells   col   ble   sub   tar   val   

原文地址:https://www.cnblogs.com/xuxingque/p/12748636.html


评论


亲,登录后才可以留言!