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;

        }
posted @ 2011-09-29 17:41  ®Geovin Du Dream Park™  阅读(546)  评论(0编辑  收藏  举报