Searching for Records in Microsoft DataGridView

  February 12, 2007

Quite often, you may need to find a particular record within the DataGridView control. This may be required, for example, to make sure that the data has been loaded into the grid correctly or to determine the position of a particular record within the grid. Currently, TestComplete does not have any special built-in routines for working with the Microsoft DataGridView control. However, DataGridView is the .NET control and can be used only within the .NET applications, which are “open” to TestComplete. This means that the internal properties and methods of DataGridView are also visible to TestComplete.

There are two ways to search for DataGridView records: you can search either within the grid’s dataset, or within the grid view itself.

Searching Within the Grid’s Dataset

If the DataGridView control is bound to an external dataset, you can search for the desired record within the dataset rather than within the grid itself. Searching within the grid’s dataset is a universal approach that is free from the grid’s user interface limitations. You can also use this approach if you need to obtain and process values in the grid cells rather than their displayed representation.

To obtain an object corresponding to DataGridView's dataset, you can use the DataSource property of the grid control. Since DataGridView can be bound to several source types (for example, to a data table or to a one-dimensional array), the object returned by the DataSource property may have a different structure in different applications, and the “path” to the exact object that holds the data may be different. To learn the structure of the dataset object, you can explore the object returned by the grid’s DataSource property in TestComplete’s Object Browser panel, or simply ask developers.

For example, if the grid is bound to the ADO.NET dataset that contains only one table, the “path” to the dataset table whose data is displayed in the grid is like this:

grid.DataSource.DataSource.Tables.Item(0)

Most .NET objects that correspond to data sources have built-in method that can be used to search for a particular value within the data source. For example, if DataGridView is bound to an object that implements the IList interface (e.g. Array, List or ArrayList), you can use the Contains method to determine whether the data source contains the specified value, and the IndexOf method to determine the position of the specified value within the data source.

If the grid data is specified by a DataTable object, you can search for a desired record within the table’s view, which can be referred via the table’s DefaultView property. To search for a particular record, you can use either the Find or FindRows method. The difference between them is that Find returns the index of the first found row that matches the search criteria, whereas FindRows returns the array of objects corresponding to all matching rows. The parameter of both methods is the sought-for value. To specify the column in which the search is performed, assign the column name to the view’s Sort property (that is, to search within the column, it must be included in the sort order). If rows are not found using the search criteria, Find returns -1 and FindRows returns an empty array (with no elements).

Below is a code snippet that illustrates how to use the Find method of the data table view to locate a particular data table row. It consists of the FindRow routine that performs the search in the data table, and the Main routine that specifies the search criteria and obtains the search results:

By the way, the DataView object supports complex queries, for example, searching by values in multiple columns. You can learn more about this feature in the “Searching a DataView” topic of MSDN.

Searching Within the Grid

Another approach that you can use to search within DataGridView, is to search within the grid itself. You can use this, for example, if the grid works in the unbound mode, or the grid is configured so that it displays data using custom formatting.

Since DataGridView does not have built-in methods for searching within the grid, you will need to implement the search algorithm yourself. A simple search algorithm implies that you iterate through the grid rows in a loop and on each iteration check whether the row meets the specified condition.

To determine the number of grid rows, you can use the grid’s RowCount property. At that, if the grid’s AllowUserToAddRows property is True, the grid also has a special row that is used to add new records to the grid (it is shown as the last row and marked with the asterisk (*) in its row header). In this case, the number of the grid’s data rows is equal to the total number of rows decreased by one.

To obtain a value in a particular row and column, you can use the following properties and methods of the grid object:

  • The grid.Item (ColIndex, RowIndex) property and grid.get_Item (ColIndex, RowIndex) method return the grid cell specified by the zero-based row and column indices.
  • The grid.Item_2 (ColName, RowIndex) property and grid.get_Item_2 (ColName, RowIndex) method return the grid cell specified by the row index and column name (which is defined in the application code).

The code snippet below demonstrates how to search within the grid itself. It locates the first grid row containing “John Doe” in the “customer” column, and posts its index to the test log. If the row is not found, an error message is posted to the log instead. The example contains the following routines:

  • The GetRowCount routine returns the number of grid rows (excluding the special “Add New Row” row).
  • The FindRowByCellValue routine locates the row by the value held in the specified column. This routine returns the index of the first found row that satisfies the search criteria or -1 if the row is not found. The column is specified by its name, which is defined in the application code.
  • Main is the main script routine.