tech-story.net

Tech Story
Menu
  • Home
  • Programing
    • Delphi
      • Database
    • Arduino
    • Android
    • Unix Socket
    • Database
  • Network
    • Mikrotik
  • software
    • File Recovery
    • Driver software
    • backup
    • download
    • Drivers
    • format converter
    • programing tools
    • internet
    • phone apps
    • Network
    • graphics
    • professional
    • DISK AND PARTITION
    • OPTIMIZER
    • utility
    • programming tools
  • Digital Coin
    • Bitcoin
    • Blockchain
  • Free Proxy
  • Privacy Policy
  • contact us
  • news
  • Health and Fitness
  • General
  • Computer
  • Games
    • android games
    • computer games
    • ps4
  • Management skills
  • Monuments
  • Series and Movies
  • electrical and electronics
  • YouTube watch
Home
Programing
Delphi
How to MultiSelect in the Delphi DBGrid
Delphi

How to MultiSelect in the Delphi DBGrid

April 19, 2017

Table of Contents

  • Delphi DBGrid
  • ALLOWING MULTIPLE SELECTIONS
  • WHEN TO USE DGMULTISELECT?
  • AN EXAMPLE

Delphi DBGrid

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

To enable multiple selection, you only need to set the dgMultiSelect element to True in the Options property. When dgMultiSelect is True, users can select multiple rows in a grid using the following techniques:
– Ctrl + Mouse clicks
– Shift + Arrow Keys

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:

  • get the number of rows selected,
  • clear the selection (unselect),
  • delete all the selected records,
  • check whether a particular record is selected.

To set dgMultiSelect to True you can either use the Object Inspector (design time) or at the run time a command like:
DBGrid1.Options:= DBGrid1.Options + [dgMultiSelect];

WHEN TO USE DGMULTISELECT?

In situations when you need an option to select random records and, for example, sum a value of a specific field in all selected records.

 

Other situations involve deletion of multiple records or similar…

AN EXAMPLE

Following the concepts set in the Beginner’s Guide to Delphi Programming, example below uses ADO components (AdoQuery connected to ADOConnection, DBGrid connected to AdoQuery over DataSource) to display the records from a database table in a DBGrid component.

procedure TForm1.btnDoSumClick(Sender: TObject);
var
  i: Integer;
  sum : Single;
begin
  if DBGrid1.SelectedRows.Count > 0 then
  begin
    sum := 0;
    with DBGrid1.DataSource.DataSet do
    begin
      for i := 0 to DBGrid1.SelectedRows.Count-1 do
      begin
        GotoBookmark(Pointer(DBGrid1.SelectedRows.Items[i]));
        sum:= sum + AdoQuery1.FieldByName('Size').AsFloat;
      end;
    end;
    edSizeSum.Text :=  FloatToStr(sum);
  end
end;

The code above uses multiple selection to get the sum of the values in the “Size” field.

That’s it. Simple and powerful. If you want to select the entire DBGrid from code, you can use this code snippet.

Share this:

  • Twitter
  • Facebook
  • LinkedIn

Related

Share
Tweet
Email
Prev Article
Next Article

Related Articles

Copy the clipboard to a stream and restore it again 1
Copy the clipboard to a stream and restore it again …

Copy the clipboard to a stream and restore it again

Create an Excel File without OLE 2
Create an Excel File without OLE const CXlsBof: array of …

Create an Excel File without OLE

Leave a Reply Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Top Posts & Pages

file password
DS CATIA P3 V5-6R2018 SP6 + Documentation
Itoo Forest Pack Pro 6.3.1 for 3ds Max 2014-2021 x64
DevExpress Universal 20.1.7 / for .NET 19.2.5 + Source 19.1.7 / VCL 19.1.2
Reallusion iClone Pro 7.82.4515.1 x64 + Plug-Ins
Corona Renderer 5 Hotfix 2 for 3DS MAX 2013-2020 / for Cinema 4D R14-R21
MATLAB R2020b Windows / macOS / Linux
HOMER Pro 3.11.2 x64
CSiCOL 9.0.1 + New Crack
Itoo RailClone Pro 3.3.1 for 3ds Max 2018-2020 x64

Categories

  • Advertisement
  • Android
  • android games
  • antivirus
  • Arduino
  • audio video editor
  • backup
  • Bitcoin
  • Blockchain
  • Chinese medicine
  • Computer
  • computer games
  • Database
  • Database
  • Delphi
  • DISK AND PARTITION
  • download
  • Driver software
  • Drivers
  • electrical and electronics
  • Fashion and beauty
  • File Recovery
  • Forex Trading
  • format converter
  • General
  • general
  • graphics
  • Health and Fitness
  • Healthy Eating
  • internet
  • Management skills
  • Mikrotik
  • Monuments
  • Network
  • News
  • Nutrition
  • Operating System
  • OPTIMIZER
  • passwords
  • phone apps
  • phone software
  • professional
  • Programing
  • programing tools
  • programming tools
  • ps4
  • Raspberry Pi
  • Series and Movies
  • software
  • Sport
  • thems
  • Uncategorized
  • Unix Socket
  • utility
  • Videos
  • YouTube watch

tech-story.net

Tech Story
Copyright © 2021 tech-story.net

Ad Blocker Detected

Our website is made possible by displaying online advertisements to our visitors. Please consider supporting us by disabling your ad blocker.

Refresh
X

You Can Close This Ad

 

 

pixel