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);
member this.Contains : obj -> bool
Public Function Contains (key As Object) As Boolean

参数

key
Object

要测试的主键的值。The value of the primary key to test for.

返回

如果集合中包含具有指定主键值的 true,则为 DataRow;否则为 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 方法,则 DataRowCollection 对象所属的 DataTable 对象必须至少有一个指定为主键列的列。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);
member this.Contains : obj[] -> bool
Public Function Contains (keys As Object()) As Boolean

参数

keys
Object[]

要测试的主键值的数组。An array of primary key values to test for.

返回

如果 true 中包含具有指定键值的 DataRowCollection,则为 DataRow;否则为 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 方法在 DataRowCollection 对象中查找特定的行。The 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 方法与值数组一起使用,则 DataRowCollection 对象所属的 DataTable 对象必须具有指定为主键的列的数组。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. 数组元素的数目必须与 DataTable中的主键列数相对应。The 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.

另请参阅

适用于