当前位置: 首页>>代码示例>>C#>>正文


C# DataTableCollection.CollectionChanging事件代码示例

本文整理汇总了C#中System.Data.DataTableCollection.CollectionChanging事件的典型用法代码示例。如果您正苦于以下问题:C# DataTableCollection.CollectionChanging事件的具体用法?C# DataTableCollection.CollectionChanging怎么用?C# DataTableCollection.CollectionChanging使用的例子?那么, 这里精选的事件代码示例或许可以为您提供帮助。您也可以进一步了解该事件所在System.Data.DataTableCollection的用法示例。


在下文中一共展示了DataTableCollection.CollectionChanging事件的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: TableCollectionCollectionChanging

public static void TableCollectionCollectionChanging()
{
    // Create a DataSet with two tables
    DataSet dataSet = new DataSet();

    // Assign the event-handler function for the 
    // CollectionChangeEvent.
    dataSet.Tables.CollectionChanging +=
        new System.ComponentModel.CollectionChangeEventHandler(
        Collection_Changing);

    // create Customer table
    DataTable customersTable = new DataTable("Customers");
    customersTable.Columns.Add("customerId",
        typeof(int)).AutoIncrement = true;
    customersTable.Columns.Add("name",
        typeof(string));
    customersTable.PrimaryKey = new DataColumn[] { customersTable.Columns["customerId"] };

    // create Orders table
    DataTable ordersTable = new DataTable("Orders");
    ordersTable.Columns.Add("orderId",
        typeof(int)).AutoIncrement = true;
    ordersTable.Columns.Add("customerId",
        typeof(int));
    ordersTable.Columns.Add("amount",
        typeof(double));
    ordersTable.PrimaryKey = new DataColumn[] { ordersTable.Columns["orderId"] };

    dataSet.Tables.AddRange(new DataTable[] { customersTable, ordersTable });

    // Check to see if each table can be removed and then
    // remove it.
    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_Changing(object sender,
    System.ComponentModel.CollectionChangeEventArgs e)
{
    // Implementing this event allows you to abort a change
    // to the collection by raising an exception which you can
    // catch.

    Console.WriteLine("Collection_Changing Event: '{0}'\table element={1}",
        e.Action.ToString(), e.Element.ToString());
}
开发者ID:.NET开发者,项目名称:System.Data,代码行数:56,代码来源:DataTableCollection.CollectionChanging


注:本文中的System.Data.DataTableCollection.CollectionChanging事件示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。