Database Programming Techniques > Selecting and Highlighting Records |
At run time, the user can select and highlight one or more records by clicking the record selector of the desired row. You can achieve the same effect in code by manipulating the grid's SelBookmarks collection, which maintains a list of bookmarks corresponding to the selected rows.
Like all other collections in True DBGrid, the SelBookmarks collection supports Add, Item, and Remove methods and a Count property. For example, to select the grid's current row, use the Add method:
TDBGrid1.SelBookmarks.Add TDBGrid1.Bookmark
After the Add method executes, the collection's Count property is incremented. You can use the Add method repeatedly to select and highlight additional rows. This is analogous to the user holding down the Ctrl key while clicking a record selector.
To deselect a single record, use the Remove method, which takes a collection index, not a bookmark:
TDBGrid1.SelBookmarks.Remove 0
After the Remove method executes, the collection's Count property is decremented. If more than one record is selected, the previous example will only remove the first selected record; that is, the one that was added to the collection first, regardless of its position on the screen. To deselect all records, you need to write a loop like the following:
With TDBGrid1.SelBookmarks
While .Count > 0
.Remove 0
Wend
End With
Tutorial 5 demonstrates how to use the SelBookmarks collection to select records that satisfy specific criteria.