DataRowCollection.Contains 方法

定義

取得值,指出集合中任何資料列的主索引鍵是否包含指定的值。

多載

Contains(Object)

取得值,指出集合中任何資料列的主索引鍵是否包含指定的值。

Contains(Object[])

取得值,指出集合中任何資料列的主索引鍵資料行是否包含在物件陣列中所指定的值。

Contains(Object)

來源:
DataRowCollection.cs
來源:
DataRowCollection.cs
來源:
DataRowCollection.cs

取得值,指出集合中任何資料列的主索引鍵是否包含指定的值。

public:
 bool Contains(System::Object ^ key);
public bool Contains (object? key);
public bool Contains (object key);
member this.Contains : obj -> bool
Public Function Contains (key As Object) As Boolean

參數

key
Object

要測試的主索引鍵值。

傳回

如果集合包含具有指定主索引鍵值的 DataRow,則為 true,否則為 false

例外狀況

資料表沒有主索引鍵。

範例

下列 Visual Basic 範例會 Contains 使用 方法來判斷物件是否 DataRowCollection 包含特定值。

 Private Sub ColContains()
    Dim table As DataTable = CType(DataGrid1.DataSource, DataTable)
    Dim rowCollection As DataRowCollection = table.Rows
    If rowCollection.Contains(Edit1.Text) Then
       Label1.Text = "At least one row contains " & Edit1.Text 
    Else
       Label1.Text = "No row contains the value in its primary key field"
    End If
End Sub

備註

若要使用 Contains 方法, DataTable 物件所屬的對象 DataRowCollection 必須至少有一個數據行指定為主鍵數據行。 如需如何建立主鍵數據行的詳細資訊, PrimaryKey 請參閱 屬性。

一旦您判斷數據列包含指定的值,就可以使用 Find 方法傳回具有值的特定 DataRow 物件。

另請參閱

適用於

Contains(Object[])

來源:
DataRowCollection.cs
來源:
DataRowCollection.cs
來源:
DataRowCollection.cs

取得值,指出集合中任何資料列的主索引鍵資料行是否包含在物件陣列中所指定的值。

public:
 bool Contains(cli::array <System::Object ^> ^ keys);
public bool Contains (object?[] keys);
public bool Contains (object[] keys);
member this.Contains : obj[] -> bool
Public Function Contains (keys As Object()) As Boolean

參數

keys
Object[]

要測試的主索引鍵值的陣列。

傳回

如果 DataRowCollection 包含具有指定索引鍵值的 DataRow,則為 true,否則為 false

例外狀況

資料表沒有主索引鍵。

範例

下列 Visual Basic 範例會 Contains 使用 方法來尋找 物件中的 DataRowCollection 特定數據列。 這個範例會建立值的陣列、資料表中每個主鍵的一個專案,然後將陣列傳遞至 方法以傳回 truefalse

Private Sub ContainsArray()
   ' This example assumes that the DataTable object contains two
   ' DataColumn objects designated as primary keys.
   ' The table has two primary key columns.
   Dim arrKeyVals(1) As Object
   Dim table As DataTable = CType(DataGrid1.DataSource, DataTable)
   Dim rowCollection As DataRowCollection = table.Rows
   arrKeyVals(0) = "Hello"
   arrKeyVals(1) = "World"
   label1.Text = rowCollection.Contains(arrKeyVals).ToString()
End Sub

備註

若要搭配值的陣列使用 Contains 方法, DataTable 物件所屬的對象 DataRowCollection 必須具有指定為主鍵的數據行陣列。 PrimaryKey如需如何建立主鍵數據行數位的詳細資訊,請參閱 屬性。 陣列元素的數目必須對應至 中的 DataTable主鍵數據行數目。

一旦您判斷數據列包含指定的值,請使用 Find 方法傳回具有 值的特定 DataRow 物件。

另請參閱

適用於