ComponentOne FlexGrid for WPF and Silverlight
Select Cells and Objects
Features > Selection > Select Cells and Objects

The Selection property is read-write, so you can select cell ranges using code. You can also perform selections using the Select method. The Select method allows you to select cells or ranges, and optionally scroll the new selection into view so the user can see it. The following code example illustrates selecting the first cell in FlexGrid and ensures the selection is visible to the user.

C#
Copy Code
// select row zero, column zero, make sure the cell is visible
grid.Select(0, 0, true);

The selection methods are based on row and column indices. But these methods can be used to make selections based on cell content. For example, the code below selects the first row that contains a given string in the grid's "Name" column.

C#
Copy Code
bool SelectName(string name)
{
  // find row that contains the given string in the "Name" column
  int col = _flexGroup.Columns["Name"].Index;
  int row = FindRow(grid, name, grid.Selection.Row, col, true);
  if (row > -1)
  {
    grid.Select(row, col);
    return true;
  }
  // not found...
  return false;
}

The code uses the FindRow helper method defined below:

C#
Copy Code
// look for a row that contains some text in a specific column
int FindRow(C1FlexGrid grid, string text,
            int startRow, int col, bool wrap)
{
  int count = grid.Rows.Count;
  for (int off = 0; off <= count; off++)
  {
    // reached the bottom and not wrapping? quit now
    if (!wrap && startRow + off >= count)
    {
      break;
    }

    // get text from row
    int row = (startRow + off) % count;
    var content = grid[row, col];

    // found? return row index
    if (content != null &&
        content.ToString().IndexOf(text,
        StringComparison.OrdinalIgnoreCase) > -1)
    {
      return row;
    }
  }

  // not found...
  return -1;
}

The FindRow method searches the specified column for a string, starting at a given row and optionally wrapping the search to start from the top if the string is not found. It is flexible enough to be used in many scenarios. Another common selection scenario is the case where you want to select a specific object in the data source. Your first impulse might be to find the index of the object in the source collection using the PagedCollectionView.IndexOf method, then use the index to select the row. The problem with that approach is that it works only if the data is not grouped. If the data is grouped, the group rows also count, so the indices of items in the data source do not match the row indices on the grid.

The easy way to solve this problem is to enumerate the rows and compare each row's DataItem property to the item you are looking for. The code below shows how this is done.

C#
Copy Code
var customer = GetSomeCustomer;

#if false // ** don't use this, won't work with grouped data

  int index = view.IndexOf(customer);
  if (index > -1)
  {
    grid.Select(index, 0);
  }

#else // this is the safe way to look for objects in the grid

  for (int row = 0; row <= grid.Rows.Count; row++)
  {
    if (row.DataItem == customer)
    {
      grid.Select(row, 0);
      break;
    }
  }
#endif
See Also

Silverlight Reference