ConstraintCollection.CollectionChanged イベント
Constraint オブジェクトを追加または削除した結果として ConstraintCollection が変更されるたびに発生します。
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 イベントを使用する方法の例を次に示します。
Private Shared Sub ConstraintCollectionChanged()
' Demonstrate ConstraintCollection.CollectionChanged event.
Try
' Create Customers table.
Dim tCustomers As DataTable = New DataTable( "Customers" )
tCustomers.Columns.Add( "id", Type.GetType("System.Int32") )
tCustomers.Columns.Add( "Name", Type.GetType("System.String") )
AddHandler tCustomers.Constraints.CollectionChanged, _
New System.ComponentModel.CollectionChangeEventHandler( AddressOf Collection_Changed )
' Create Orders table.
Dim tOrders As DataTable = New DataTable( "Orders" )
tOrders.Columns.Add( "CustID", Type.GetType("System.Int32") )
tOrders.Columns.Add( "CustName", Type.GetType("System.String") )
AddHandler tOrders.Constraints.CollectionChanged, _
New System.ComponentModel.CollectionChangeEventHandler( AddressOf Collection_Changed )
' Create unique constraint.
Dim uc As UniqueConstraint = New UniqueConstraint(tCustomers.Columns("id"))
tCustomers.Constraints.Add(uc)
' Create unique constraint and specify as primary key.
tOrders.Constraints.Add("pKey", tOrders.Columns("CustID"), True )
' Remove constraints.
tCustomers.Constraints.RemoveAt( 0 )
' Results in an Exception. You can't remove a primary key constraint.
tOrders.Constraints.RemoveAt( 0 )
Catch e As Exception
' Process exception and return.
Dim log As System.Diagnostics.EventLog = New System.Diagnostics.EventLog()
log.Source = "My Application"
log.WriteEntry(e.ToString())
Console.WriteLine("Exception of type {0} occurred.", e.GetType().ToString())
End Try
End Sub
Private Shared Sub Collection_Changed( sender As object, e As System.ComponentModel.CollectionChangeEventArgs )
Console.WriteLine( "List_Changed Event: '{0}'\t element={1}", e.Action, e.Element )
End Sub
[C#]
private static void ConstraintCollectionChanged()
{
// Demonstrate ConstraintCollection.CollectionChanged event.
try
{
// Create Customers table.
DataTable tCustomers = new DataTable( "Customers" );
tCustomers.Columns.Add( "id", typeof(int) );
tCustomers.Columns.Add( "Name", typeof(string) );
tCustomers.Constraints.CollectionChanged +=
new System.ComponentModel.CollectionChangeEventHandler( Collection_Changed );
// Create Orders table.
DataTable tOrders = new DataTable( "Orders" );
tOrders.Columns.Add( "CustID", typeof(int) );
tOrders.Columns.Add( "CustName", typeof(string) );
tOrders.Constraints.CollectionChanged +=
new System.ComponentModel.CollectionChangeEventHandler( Collection_Changed );
// Create unique constraint.
UniqueConstraint uc = new UniqueConstraint(tCustomers.Columns["id"]);
tCustomers.Constraints.Add(uc);
// Create unique constraint and specify as primary key.
tOrders.Constraints.Add("pKey", tOrders.Columns["CustID"], true );
// Remove constraints.
tCustomers.Constraints.RemoveAt( 0 );
// Results in an Exception. You can't remove a primary key constraint.
tOrders.Constraints.RemoveAt( 0 );
}
catch(Exception e)
{
// Process exception and return.
System.Diagnostics.EventLog log = new System.Diagnostics.EventLog();
log.Source = "My Application";
log.WriteEntry(e.ToString());
Console.WriteLine("Exception of type {0} occurred.", e.GetType());
}
}
private static void Collection_Changed( object sender, System.ComponentModel.CollectionChangeEventArgs e )
{
Console.WriteLine( "List_Changed Event: '{0}'\t element={1}", e.Action, e.Element );
}
[C++]
public:
static void ConstraintCollectionChanged() {
// Demonstrate ConstraintCollection::CollectionChanged event.
try {
// Create Customers table.
DataTable* tCustomers = new DataTable(S"Customers");
tCustomers->Columns->Add(S"id", __typeof(int));
tCustomers->Columns->Add(S"Name", __typeof(String));
tCustomers->Constraints->CollectionChanged += new System::ComponentModel::CollectionChangeEventHandler(0, Collection_Changed);
// Create Orders table.
DataTable* tOrders = new DataTable(S"Orders");
tOrders->Columns->Add(S"CustID", __typeof(int));
tOrders->Columns->Add(S"CustName", __typeof(String));
tOrders->Constraints->CollectionChanged += new System::ComponentModel::CollectionChangeEventHandler(0, Collection_Changed);
// Create unique constraint.
UniqueConstraint* uc = new UniqueConstraint(tCustomers->Columns->Item[S"id"]);
tCustomers->Constraints->Add(uc);
// Create unique constraint and specify as primary key.
tOrders->Constraints->Add(S"pKey", tOrders->Columns->Item[S"CustID"], true);
// Remove constraints.
tCustomers->Constraints->RemoveAt(0);
// Results in an Exception. You can't remove a primary key constraint.
tOrders->Constraints->RemoveAt(0);
} catch (Exception* e) {
// Process exception and return.
System::Diagnostics::EventLog* log = new System::Diagnostics::EventLog();
log->Source = S"My Application";
log->WriteEntry(e->ToString());
Console::WriteLine(S"Exception of type {0} occurred.", e->GetType());
}
}
private:
static void Collection_Changed(Object* /*sender*/, System::ComponentModel::CollectionChangeEventArgs* e) {
Console::WriteLine(S"List_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 ファミリ
参照
ConstraintCollection クラス | ConstraintCollection メンバ | System.Data 名前空間