Working with FlexGrid > Selection > Selection and Selection Modes > Selecting cells and objects using code |
The Selection property is read-write, so you can select cell ranges using code. You can also perform selections using the Select method, like in the C1FlexGrid for WinForms control. The Select method allows you to select cells or ranges, and optionally scroll the new selection into view so the user can see it.
For example, to select the first cell in the grid and make sure it is visible to the user, use this code:
C# |
Copy Code
|
---|---|
// select row zero, column zero, make sure the cell is visible fg.Select(0, 0, true); |
The selection methods all work based on row and column indices. But you can use them to make selections based on cell content as well. 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(_flex, name, _flex.Selection.Row, col, true); if (row > -1) { _flex.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 flex, string text, int startRow, int col, bool wrap) { int count = flex.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 = flex[row, col]; // found? return row index if (content != null && content.ToString().IndexOf(text, StringComparison.OrdinalIgnoreCase) > -1) { return row; } } // not found... return -1; } |
This FindRow method implements the functionality provided by the FindRow method in the WinForms version of the C1FlexGrid. That method is not built into the Silverlight or WPF version of the grid in order to keep the control footprint small. The 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) { _flex.Select(index, 0); } #else // this is the safe way to look for objects in the grid for (int row = 0; row <= _flex.Rows.Count; row++) { if (row.DataItem == customer) { _flex.Select(row, 0); break; } } #endif |