欢迎光临

木子屋

标题: 获取DataTable单元格修改前的值示例

作者: dnawo

日期: 2009-10-28 03:39 PM

分类: Win编程

内容: 复制内容到剪贴板 程序代码//创建 DataTable
DataTable categories = new DataTable("Categories");
//添加字段
DataColumn column = new DataColumn();
column.DataType = System.Type.GetType("System.Int32");
column.ColumnName = "CategoryID";
column.AutoIncrement = true;
column.AutoIncrementSeed = 1;
column.AutoIncrementStep = 1;
categories.Columns.Add(column);
column = new DataColumn();
column.DataType = System.Type.GetType("System.String");
column.ColumnName = "CategoryName";
categories.Columns.Add(column);
//设置主键
DataColumn[] pKey = new DataColumn[1];
pKey[0] = categories.Columns["CategoryID"];
categories.PrimaryKey = pKey;
//添加记录
DataRow newRow = categories.NewRow();
newRow["CategoryName"] = "Category1";
categories.Rows.Add(newRow);
newRow = categories.NewRow();
newRow["CategoryName"] = "Category2";
categories.Rows.Add(newRow);
//提交更新
categories.AcceptChanges();
for (int i = 0; i < categories.Rows.Count; i++)
textBox1.Text += categories.Rows[i].RowState + "\r\n";
textBox1.Text += "----------------------------------\r\n";
//修改值
categories.Rows[0]["CategoryName"] = "New Category1";
for (int i = 0; i < categories.Rows.Count; i++)
textBox1.Text += categories.Rows[i].RowState + "\r\n";
textBox1.Text += "----------------------------------\r\n";
//获取原始值(修改前的值)
if(categories.Rows[0].HasVersion(DataRowVersion.Original))
textBox1.Text += categories.Rows[0]["CategoryName", DataRowVersion.Original];
结果：
引用内容Unchanged
Unchanged
----------------------------------
Modified
Unchanged
----------------------------------
Category1

+ 查看当前日志评论 (0)

登录

────────────

木子屋

PJBlog3 v3.2.9.518 Inside.

Processed In 2.250 ms

返回

标题: 获取DataTable单元格修改前的值示例

你没有权限发表评论

────────────

木子屋

PJBlog3 v3.2.9.518 Inside.

Processed In 2.250 ms

返回

暂无评论

返回

────────────

木子屋

PJBlog3 v3.2.9.518 Inside.

Processed In 2.313 ms

返回