Datatable 与Datagridview相关操作

	/// 
        /// 方法实现把dgv里的数据完整的复制到一张内存表
        /// 
        /// dgv控件作为参数
        /// 返回临时内存表
        public static 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] = dgv.Rows[count].Cells[countsub].Value.ToString();
                }
                dt.Rows.Add(dr);
            }
            return dt;
        }

你可能感兴趣的:(Datatable 与Datagridview相关操作)