本文整理汇总了C#中System.Data.DataTableCollection.CollectionChanged事件的典型用法代码示例。如果您正苦于以下问题:C# DataTableCollection.CollectionChanged事件的具体用法?C# DataTableCollection.CollectionChanged怎么用?C# DataTableCollection.CollectionChanged使用的例子?那么, 这里精选的事件代码示例或许可以为您提供帮助。您也可以进一步了解该事件所在类System.Data.DataTableCollection
的用法示例。
在下文中一共展示了DataTableCollection.CollectionChanged事件的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: TableCollectionCollectionChanged
public static void TableCollectionCollectionChanged()
{
// create a DataSet with two tables
DataSet dataSet = new DataSet();
dataSet.Tables.CollectionChanged +=
new System.ComponentModel.CollectionChangeEventHandler(
Collection_Changed);
// create Customer table
DataTable custTable = new DataTable("Customers");
custTable.Columns.Add("customerId",
typeof(int)).AutoIncrement = true;
custTable.Columns.Add("name",
typeof(string));
custTable.PrimaryKey = new DataColumn[] { custTable.Columns["customerId"] };
// create Orders table
DataTable orderTable = new DataTable("Orders");
orderTable.Columns.Add("orderId",
typeof(int)).AutoIncrement = true;
orderTable.Columns.Add("customerId", typeof(int));
orderTable.Columns.Add("amount", typeof(double));
orderTable.PrimaryKey = new DataColumn[] { orderTable.Columns["orderId"] };
dataSet.Tables.AddRange(new DataTable[] { custTable, orderTable });
// remove all tables
// check if table can be removed and then
// remove it, cannot use a foreach when
// removing items from a collection
// equivalent to dataSet.Tables.Clear()
while (dataSet.Tables.Count > 0)
{
DataTable table = dataSet.Tables[0];
if (dataSet.Tables.CanRemove(table))
{
dataSet.Tables.RemoveAt(0);
}
}
Console.WriteLine("dataSet has {0} tables",
dataSet.Tables.Count);
}
private static void Collection_Changed(object sender,
System.ComponentModel.CollectionChangeEventArgs e)
{
Console.WriteLine("Collection_Changed Event: '{0}'\table element={1}",
e.Action.ToString(), e.Element.ToString());
}