| DBGrid with MultiSelect | ||||||||||||||
| Multiple row selection in Delphi DBGrid - providing the ability to select multiple records within the grid. | ||||||||||||||
Delphi's DBGrid is one of the mostly used DB aware component in database related applications. It's main purpose is to display and enable users of your application to manipulate records from a dataset in a tabular grid.
One of the lesser known features of the DBGrid component is that you can set it to allow multiple row selection. What this means is that the users of your application will have the ability to select multiple records (rows) from the dataset connected to grid.
Allowing multiple selections The (rows) records selected are represented as bookmarks and are stored in the grid's SelectedRows property. Note that SelectedRows is only meaningful when the Options property includes dgMultiSelect and dgRowSelect (both are set to True). On the other hand, when using dgRowSelect (individual cells cannot be selected) the user won't be able to edit records directly through the grid - dgEditing is automatically set to False.
The SelectedRows property is an object of type TBookmarkList. We can use the SelectedRows property to, for example: To set dgMultiSelect to True you can either use the Object Inspector (design time) or at the run time a command like:
When to use dgMultiSelect?
The code above uses multiple selection to get the sum of the values in the "Size" field. The picture shows the code in action:
![]() That's it. Simple and powerful. If you want to select the entire DBGrid from code, you can use this code snippet. To see how to add colors to DBGrid, see the Coloring DBGrid article; of course don't miss the "Using the DBGrid" article collection! A Beginner's Guide to Delphi Database Programming >> |
||||||||||||||

