C# 把DataGridView控件数据,转成DataTable

 

                DataTable dt = new DataTable();
                //把DataGridView控件数据,转成DataTable
                for (int count = 0; count < DataGridView.Columns.Count; count++)
                {
                    DataColumn dc = new DataColumn(DataGridView.Columns[count].Name.ToString());
                          dt.Columns.Add(dc);
                }
                for (int count = 0; count < DataGridView.Rows.Count; count++)
                {
                      DataRow dr = dt.NewRow();
                      for (int countsub = 0; countsub < DataGridView.Columns.Count; countsub++)
                       {
                           dr[countsub] = Convert.ToString(DataGridView.Rows[count].Cells[countsub].Value);
                         }
                         dt.Rows.Add(dr);
                }

 

 

 其实强转也可以

 

DataTable dt = DataGridView.DataSource as DataTable;

 

 

 

转: https://blog.csdn.net/yeyingss/article/details/50922763

 

posted @ 2021-06-21 09:36  与f  阅读(1057)  评论(0编辑  收藏  举报