DataTable オブジェクトを追加または削除した結果として DataTableCollection が変更されているときに発生します。
Public Event CollectionChanging As CollectionChangeEventHandler
[C#]
public event CollectionChangeEventHandler CollectionChanging;
[C++]
public: __event CollectionChangeEventHandler* CollectionChanging;
[JScript] JScript では、このクラスで定義されているイベントを処理できます。ただし、独自に定義することはできません。
イベント データ
イベント ハンドラが、このイベントに関連するデータを含む、CollectionChangeEventArgs 型の引数を受け取りました。次の CollectionChangeEventArgs プロパティには、このイベントの固有の情報が記載されます。
プロパティ | 説明 |
---|---|
Action | コレクションがどのように変更されたかを示すアクションを取得します。 |
Element | 変更されたコレクションのインスタンスを取得します。 |
解説
イベント処理の詳細については、「 イベントの利用 」を参照してください。
使用例
[Visual Basic, C#, C++] CollectionChanging イベントを使用する方法の例を次に示します。
Public Sub TableCollectionCollectionChanging()
' Create a DataSet with two tables
Dim myDataSet As DataSet = New DataSet()
AddHandler myDataSet.Tables.CollectionChanging, AddressOf Collection_Changing
' Create Customer table
Dim t As DataTable = New DataTable("Customers")
t.Columns.Add("customerId", System.Type.GetType("System.Integer")).AutoIncrement = True
t.Columns.Add("name", System.Type.GetType("System.String"))
t.PrimaryKey = New DataColumn() {t.Columns("customerId")}
' Create Orders table
Dim t2 As DataTable = New DataTable("Orders")
t2.Columns.Add("orderId", System.Type.GetType("System.Integer")).AutoIncrement = True
t2.Columns.Add("customerId", System.Type.GetType("System.Integer"))
t2.Columns.Add("amount", System.Type.GetType("System.Double"))
t2.PrimaryKey = New DataColumn() {t.Columns("orderId")}
' Add the tables to the DataTableCollection
myDataSet.Tables.AddRange(New DataTable() {t, t2})
' Remove all tables
' First check to see if the table can be removed and
' then remove it.
'
' You cannot use a For Each loop to remove items
' from a collection.
Do While (myDataSet.Tables.Count > 0)
Dim table As DataTable
table = myDataSet.Tables(0)
If (myDataSet.Tables.CanRemove(table)) Then
myDataSet.Tables.RemoveAt(0)
End If
Loop
Console.WriteLine("myDataSet has {0} tables", myDataSet.Tables.Count)
End Sub
Private Sub Collection_Changing(ByVal sender As Object, ByVal e As System.ComponentModel.CollectionChangeEventArgs)
' 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}'\t element={1}", _
e.Action.ToString(), e.Element.ToString())
End Sub
[C#]
public static void TableCollectionCollectionChanging()
{
// Create a DataSet with two tables
DataSet myDataSet = new DataSet();
// Assign the event-handler function for the CollectionChangeEvent.
myDataSet.Tables.CollectionChanging +=
new System.ComponentModel.CollectionChangeEventHandler( Collection_Changing );
// create Customer table
DataTable t = new DataTable( "Customers" );
t.Columns.Add( "customerId", typeof(int) ).AutoIncrement = true;
t.Columns.Add( "name", typeof(string) );
t.PrimaryKey = new DataColumn[] { t.Columns["customerId"] };
// create Orders table
DataTable t2 = new DataTable( "Orders" );
t2.Columns.Add( "orderId", typeof(int) ).AutoIncrement = true;
t2.Columns.Add( "customerId", typeof(int) );
t2.Columns.Add( "amount", typeof(double) );
t2.PrimaryKey = new DataColumn[] { t.Columns["orderId"] };
myDataSet.Tables.AddRange( new DataTable[] {t, t2} );
// Check to see if each table can be removed and then
// remove it.
while( myDataSet.Tables.Count > 0 )
{
DataTable table = myDataSet.Tables[0];
if( myDataSet.Tables.CanRemove( table ) )
{
myDataSet.Tables.RemoveAt( 0 );
}
}
Console.WriteLine( "myDataSet has {0} tables", myDataSet.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}'\t element={1}",
e.Action.ToString(), e.Element.ToString() );
}
[C++]
public:
static void TableCollectionCollectionChanging()
{
// Create a DataSet with two tables
DataSet* myDataSet = new DataSet();
// Assign the event-handler function for the CollectionChangeEvent.
myDataSet->Tables->CollectionChanging +=
new System::ComponentModel::CollectionChangeEventHandler(0, Collection_Changing );
// create Customer table
DataTable* t = new DataTable( S"Customers" );
t->Columns->Add( S"customerId", __typeof(int) )->AutoIncrement = true;
t->Columns->Add( S"name", __typeof(String) );
DataColumn* temp1 [] = {t->Columns->Item[S"customerId"]};
t->PrimaryKey = temp1;
// create Orders table
DataTable* t2 = new DataTable( S"Orders" );
t2->Columns->Add( S"orderId", __typeof(int) )->AutoIncrement = true;
t2->Columns->Add( S"customerId", __typeof(int) );
t2->Columns->Add( S"amount", __typeof(double) );
DataColumn* temp2 [] = {t->Columns->Item[S"orderId"]};
t2->PrimaryKey = temp2;
DataTable* temp0 [] = {t, t2};
myDataSet->Tables->AddRange( temp0 );
// Check to see if each table can be removed and then
// remove it.
while( myDataSet->Tables->Count > 0 )
{
DataTable* table = myDataSet->Tables->Item[0];
if( myDataSet->Tables->CanRemove( table ) )
{
myDataSet->Tables->RemoveAt( 0 );
}
}
Console::WriteLine( S"myDataSet has {0} tables", __box(myDataSet->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( S"Collection_Changing Event: '{0}'\t element={1}",
__box(e->Action), e->Element );
}
[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン をクリックします。
必要条件
プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ
参照
DataTableCollection クラス | DataTableCollection メンバ | System.Data 名前空間