DataTable オブジェクトを追加または削除した結果として DataTableCollection が変更された後に発生します。
Public Event CollectionChanged As CollectionChangeEventHandler
[C#]
public event CollectionChangeEventHandler CollectionChanged;
[C++]
public: __event CollectionChangeEventHandler* CollectionChanged;
[JScript] JScript では、このクラスで定義されているイベントを処理できます。ただし、独自に定義することはできません。
イベント データ
イベント ハンドラが、このイベントに関連するデータを含む、CollectionChangeEventArgs 型の引数を受け取りました。次の CollectionChangeEventArgs プロパティには、このイベントの固有の情報が記載されます。
プロパティ | 説明 |
---|---|
Action | コレクションがどのように変更されたかを示すアクションを取得します。 |
Element | 変更されたコレクションのインスタンスを取得します。 |
解説
イベント処理の詳細については、「 イベントの利用 」を参照してください。
使用例
[Visual Basic, C#, C++] CollectionChanged イベントを使用する方法の例を次に示します。
Public Sub TableCollectionCollectionChanged()
' create a DataSet with two tables
Dim myDataSet As DataSet = New DataSet()
AddHandler myDataSet.Tables.CollectionChanging, AddressOf Collection_Changed
' 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")}
myDataSet.Tables.AddRange(New DataTable() {t, t2})
' Now remove all tables.
' First check to see if the table can be removed,
' then remove it from the collection.
'
' You cannot use a For Each loop when
' removing items from a collection.
Do While (myDataSet.Tables.Count > 0)
Dim table As DataTable = 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_Changed(ByVal sender As Object, ByVal e As System.ComponentModel.CollectionChangeEventArgs)
Console.WriteLine("Collection_Changed Event: '{0}'\t element={1}", _
e.Action.ToString(), e.Element.ToString())
End Sub
[C#]
public static void TableCollectionCollectionChanged()
{
// create a DataSet with two tables
DataSet myDataSet = new DataSet();
myDataSet.Tables.CollectionChanged +=
new System.ComponentModel.CollectionChangeEventHandler( Collection_Changed );
// 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} );
// 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 myDataSet.Tables.Clear()
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_Changed(object sender, System.ComponentModel.CollectionChangeEventArgs e)
{
Console.WriteLine( "Collection_Changed Event: '{0}'\t element={1}",
e.Action.ToString(), e.Element.ToString() );
}
[C++]
public:
static void TableCollectionCollectionChanged()
{
// create a DataSet with two tables
DataSet* myDataSet = new DataSet();
myDataSet->Tables->CollectionChanged +=
new System::ComponentModel::CollectionChangeEventHandler(0, Collection_Changed );
// 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* temp0 [] = {t->Columns->Item[S"customerId"]};
t->PrimaryKey = temp0;
// 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* temp1 [] = {t->Columns->Item[S"orderId"]};
t2->PrimaryKey = temp1;
DataTable* temp2 [] = {t, t2};
myDataSet->Tables->AddRange( temp2 );
// 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 myDataSet.Tables.Clear()
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_Changed(Object* /*sender*/, System::ComponentModel::CollectionChangeEventArgs* e)
{
Console::WriteLine( S"Collection_Changed 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 名前空間