20161013001 DataGridView 数据转 DataTable

    DataTable dt2 =  GetDgvToTable(Form_CY_ProjectRequirements_D);
    MessageBox.Show( dt2.Rows.Count.ToString());

    ////

    protected  DataTable GetDgvToTable(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;
        }

posted @ 2016-10-13 15:23  追逐苦痛  阅读(170)  评论(0编辑  收藏  举报