csharp datagridview to a datatable,a dataset

来源:互联网 发布:淘宝减肥产品销量排行 编辑:程序博客网 时间:2024/05/16 10:22
 /// <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;        }


原创粉丝点击