csharp datagridview to a datatable,a dataset

 /// <summary>
        /// datagridview to a datatable
        /// geovindu 涂聚文
        /// </summary>
        /// <param name="dataGridView"></param>
        /// <returns></returns>
        private DataSet DataGridViewToDatSet(DataGridView dataGridView)
        {
            DataSet ds = new DataSet();
            DataTable dt = new DataTable();
            dt.TableName = "StaffStateList";
            foreach (DataGridViewColumn col in dataGridView.Columns)
            {
                dt.Columns.Add(col.DataPropertyName, col.ValueType);
            }
            foreach (DataGridViewRow gridRow in dataGridView.Rows)
            {
                if (gridRow.IsNewRow)
                    continue;

                DataRow dtRow = dt.NewRow();
                for (int i1 = 0; i1 < dataGridView.Columns.Count-1; i1++)
                    dtRow[i1] = (gridRow.Cells[i1].Value == null ? DBNull.Value : gridRow.Cells[i1].Value);
                dt.Rows.Add(dtRow);
            }
            ds.Tables.Add(dt);
            //System.Diagnostics.Debugger.Break();
            return ds;

        }
哲学管理(学)人生, 文学艺术生活, 自动(计算机学)物理(学)工作, 生物(学)化学逆境, 历史(学)测绘(学)时间, 经济(学)数学金钱(理财), 心理(学)医学情绪, 诗词美容情感, 美学建筑(学)家园, 解构建构(分析)整合学习, 智商情商(IQ、EQ)运筹(学)成功.---Geovin Du(涂聚文)
原文地址:https://www.cnblogs.com/geovindu/p/2195745.html