From:MSDN Example
private static void DemonstrateMergeTable()
{
// Demonstrate merging, within and without
// preserving changes.

// In this example, take these actions:
// 1. Create a DataTable (table1) and fill the table with data.
// 2. Create a copy of table1, and modify its data (modifiedTable).
// 3. Modify data in table1.
// 4. Make a copy of table1 (table1Copy).
// 5. Merge the data from modifiedTable into table1 and table1Copy,
// showing the difference between setting the preserveChanges
// parameter to true and false.

// Create a new DataTable.
DataTable table1 = new DataTable("Items");

// Add two columns to the table:
DataColumn column = new DataColumn("id", typeof(System.Int32));
column.AutoIncrement = true;
table1.Columns.Add(column);

column = new DataColumn("item", typeof(System.String));
table1.Columns.Add(column);

// Set primary key column.
table1.PrimaryKey = new DataColumn[] { table1.Columns[0] };

// Add some rows.
DataRow row;
for (int i = 0; i <= 3; i++)
{
row = table1.NewRow();
row["item"] = "Item " + i;
table1.Rows.Add(row);
}

// Accept changes.
table1.AcceptChanges();
PrintValues(table1, "Original values");

// Using the same schema as the original table,
// modify the data for later merge.
DataTable modifiedTable = table1.Copy();
foreach (DataRow rowModified in modifiedTable.Rows)
{
rowModified["item"] = rowModified["item"].ToString()
+ " modified";
}
modifiedTable.AcceptChanges();

// Change row values, and add a new row:
table1.Rows[0]["item"] = "new Item 0";
table1.Rows[1]["item"] = "new Item 1";

row = table1.NewRow();
row["id"] = 4;
row["item"] = "Item 4";
table1.Rows.Add(row);

// Get a copy of the modified data:
DataTable table1Copy = table1.Copy();
PrintValues(table1, "Modified and new Values");
PrintValues(modifiedTable, "Data to be merged into table1");

// Merge new data into the modified data.
table1.Merge(modifiedTable, true);
PrintValues(table1, "Merged data (preserve changes)");

table1Copy.Merge(modifiedTable, false);
PrintValues(table1Copy, "Merged data (don't preserve changes)");
}
private static void PrintValues(DataTable table, string lbl)
{
// Display the values in the supplied DataTable:

System.Web.HttpContext.Current.Response.Write(lbl+"<br />");
foreach (DataRow row in table.Rows)
{
foreach (DataColumn column in table.Columns)
{
System.Web.HttpContext.Current.Response.Write(row[column, DataRowVersion.Current]);
}
System.Web.HttpContext.Current.Response.Write("<br />");
}
}
private static void DemonstrateMergeTable()
{
// Demonstrate merging, within and without
// preserving changes.
// In this example, take these actions:
// 1. Create a DataTable (table1) and fill the table with data.
// 2. Create a copy of table1, and modify its data (modifiedTable).
// 3. Modify data in table1.
// 4. Make a copy of table1 (table1Copy).
// 5. Merge the data from modifiedTable into table1 and table1Copy,
// showing the difference between setting the preserveChanges
// parameter to true and false.
// Create a new DataTable.
DataTable table1 = new DataTable("Items");
// Add two columns to the table:
DataColumn column = new DataColumn("id", typeof(System.Int32));
column.AutoIncrement = true;
table1.Columns.Add(column);
column = new DataColumn("item", typeof(System.String));
table1.Columns.Add(column);
// Set primary key column.
table1.PrimaryKey = new DataColumn[] { table1.Columns[0] };
// Add some rows.
DataRow row;
for (int i = 0; i <= 3; i++)
{
row = table1.NewRow();
row["item"] = "Item " + i;
table1.Rows.Add(row);
}
// Accept changes.
table1.AcceptChanges();
PrintValues(table1, "Original values");
// Using the same schema as the original table,
// modify the data for later merge.
DataTable modifiedTable = table1.Copy();
foreach (DataRow rowModified in modifiedTable.Rows)
{
rowModified["item"] = rowModified["item"].ToString()
+ " modified";
}
modifiedTable.AcceptChanges();
// Change row values, and add a new row:
table1.Rows[0]["item"] = "new Item 0";
table1.Rows[1]["item"] = "new Item 1";
row = table1.NewRow();
row["id"] = 4;
row["item"] = "Item 4";
table1.Rows.Add(row);
// Get a copy of the modified data:
DataTable table1Copy = table1.Copy();
PrintValues(table1, "Modified and new Values");
PrintValues(modifiedTable, "Data to be merged into table1");
// Merge new data into the modified data.
table1.Merge(modifiedTable, true);
PrintValues(table1, "Merged data (preserve changes)");
table1Copy.Merge(modifiedTable, false);
PrintValues(table1Copy, "Merged data (don't preserve changes)");
}
private static void PrintValues(DataTable table, string lbl)
{
// Display the values in the supplied DataTable:
System.Web.HttpContext.Current.Response.Write(lbl+"<br />");
foreach (DataRow row in table.Rows)
{
foreach (DataColumn column in table.Columns)
{
System.Web.HttpContext.Current.Response.Write(row[column, DataRowVersion.Current]);
}
System.Web.HttpContext.Current.Response.Write("<br />");
}
}posted @ 2008-05-04 20:01 roboth 阅读(23) 评论(1) 编辑
//Create DataTable
DataTable dt = new DataTable();
dt.CaseSensitive = true;
//Create DataColumns
DataColumn col1 = new DataColumn("id", typeof(int));
DataColumn col2 = new DataColumn("UserName", typeof(string));
//Config Propertyies
col2.AllowDBNull = false;
col2.MaxLength = 50;
col2.Unique = true;

col1.AutoIncrement = true;
col1.AutoIncrementSeed = 100;
col1.AutoIncrementStep = 1;
col1.ReadOnly = true;
//the DataColumn with Expression
DataColumn col3 = new DataColumn("Expression", typeof(string));
col3.Expression = "id+UserName";
//add DataColumns to dt;
dt.Columns.Add(col1);
dt.Columns.Add(col2);
dt.Columns.Add(col3);
dt.PrimaryKey = new DataColumn[] { dt.Columns[0] };
//Create test data
DataRow dr = dt.NewRow();
dr[1] = "Roboth";
dt.Rows.Add(dr);
dr = dt.NewRow();
dr[1] = "xinsoft";
dt.Rows.Add(dr);
dr = dt.NewRow();
dr[1] = "SpiderMan";
dt.Rows.Add(dr);
//bind to gridview
this.gridview1.DataSource = dt;
this.gridview1.DataBind();
//example1 for edit
DataRow dr1 = dt.Rows.Find(102);
if (dr1 == null)
{
Response.Write("no find");
}
else
{
dr1.BeginEdit();
dr1[1] = "SpiderWoman";
dr1.EndEdit();
}
//example2 for edit
object[] obj = new object[] {null,"Spo",null };
DataRow dr2 = dt.Rows.Find(102);
if (dr2.IsNull(2))//赋值是 DBNull.Value
{
dr2[2] = "test";
}
dr2.ItemArray = obj;
//example for delete
DataRow dr_D = dt.Rows.Find(102);
dr_D.Delete();
//Remove corresponding row
dt.Rows.Remove(dt.Rows.Find(101));
////Remove corresponding row again
dt.Rows.RemoveAt(0);
this.gridview2.DataSource = dt;
this.gridview2.DataBind();
DataSet ds = new DataSet();
Response.Write(ds.DataSetName);posted @ 2008-05-04 18:51 roboth 阅读(66) 评论(0) 编辑


