DataRowCollection.Contains 方法

定義

取得值,指出集合中任何資料列的主索引鍵是否包含指定的值。Gets a value that indicates whether the primary key columns of any row in the collection contain the specified value.

多載

Contains(Object)

取得值,指出集合中任何資料列的主索引鍵是否包含指定的值。Gets a value that indicates whether the primary key of any row in the collection contains the specified value.

Contains(Object[])

取得值,指出集合中任何資料列的主索引鍵資料行是否包含在物件陣列中所指定的值。Gets a value that indicates whether the primary key columns of any row in the collection contain the values specified in the object array.

Contains(Object)

取得值,指出集合中任何資料列的主索引鍵是否包含指定的值。Gets a value that indicates whether the primary key of any row in the collection contains the specified value.

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

要測試的主索引鍵值。The value of the primary key to test for.

傳回

Boolean

如果集合包含具有指定主索引鍵值的 DataRow,則為 true,否則為 falsetrue if the collection contains a DataRow with the specified primary key value; otherwise, false.

例外狀況

資料表沒有主索引鍵。The table does not have a primary key.

範例

下列 Visual Basic 範例會使用 Contains 方法來判斷 DataRowCollection 物件是否包含特定的值。The following Visual Basic example uses the Contains method to determine whether a DataRowCollection object contains a specific value.

 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 物件所屬的物件必須至少要有一個資料行指定為主鍵資料行。To use the Contains method, the DataTable object to which the DataRowCollection object belongs to must have at least one column designated as a primary key column. 如需 PrimaryKey 如何建立主鍵資料行的詳細資訊,請參閱屬性。See the PrimaryKey property for more information about how to create a primary key column.

一旦確定某個資料列包含指定的值,您就可以使用 Find 方法傳回 DataRow 具有值的特定物件。As soon as you have determined that a row contains the specified value, you can use the Find method to return the specific DataRow object that has the value.

另請參閱

適用於

Contains(Object[])

取得值,指出集合中任何資料列的主索引鍵資料行是否包含在物件陣列中所指定的值。Gets a value that indicates whether the primary key columns of any row in the collection contain the values specified in the object array.

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[]

要測試的主索引鍵值的陣列。An array of primary key values to test for.

傳回

Boolean

如果 DataRowCollection 包含具有指定索引鍵值的 DataRow,則為 true,否則為 falsetrue if the DataRowCollection contains a DataRow with the specified key values; otherwise, false.

例外狀況

資料表沒有主索引鍵。The table does not have a primary key.

範例

下列 Visual Basic 範例會使用 Contains 方法來尋找物件中的特定資料列 DataRowCollectionThe following Visual Basic example uses the Contains method to find a particular row in a DataRowCollection object. 此範例會建立值陣列、資料表中每個主鍵的一個元素,然後將陣列傳遞給方法,以傳回 truefalseThe example creates an array of values, one element for each primary key in the table, and then passes the array to the method to return a true or false.

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 物件所屬的物件必須具有指定為主鍵的資料行陣列。To use the Contains method with an array of values, the DataTable object to which the DataRowCollection object belongs must have an array of columns designated as primary keys. 如需 PrimaryKey 如何建立主鍵資料行陣列的詳細資訊,請參閱屬性。See the PrimaryKey property for more information about how to create an array of primary key columns. 陣列元素的數目必須對應至中的主鍵資料行數目 DataTableThe number of array elements must correspond to the number of primary key columns in the DataTable.

一旦您確定某個資料列包含指定的值,請使用 Find 方法傳回 DataRow 具有值的特定物件。As soon as you have determined that a row contains the specified value, use the Find method to return the specific DataRow object that has the value.

另請參閱

適用於