When you add [dgMultiSelect] to the Options property of a DBGrid, you give yourself the ability to select multiple records within the grid.
    The records you select are represented as bookmarks and are stored in the SelectedRows property.
    The SelectedRows property is an object of type TBookmarkList.  The properties and methods are described below.

// property SelectedRows: TBookmarkList read FBookmarks;

//   TBookmarkList = class
//   public
     {* The Clear method will free all the selected records within the DBGrid *}
     // procedure Clear;
     {* The Delete method will delete all the selected rows from the dataset *}
     // procedure Delete;
     {* The Find method determines whether a bookmark is in the selected list. *}
     // function  Find(const Item: TBookmarkStr;
     //      var Index: Integer): Boolean;
     {* The IndexOf method returns the index of the bookmark within the Items property. *}
     // function IndexOf(const Item: TBookmarkStr): Integer;
     {* The Refresh method returns a boolean value to notify whether any orphans were dropped (deleted) during the time the record has been selected in the grid.  The refresh method can be used to update the selected list to minimize the possibility of accessing a deleted record. *}
     // function  Refresh: Boolean;  True = orphans found
     {* The Count property returns the number of currently selected items in the DBGrid *}
     // property Count: Integer read GetCount;
     {* The CurrentRowSelected property returns a boolean value and determines whether the current row is selected or not. *}
     // property CurrentRowSelected: Boolean
     //      read GetCurrentRowSelected
     //      write SetCurrentRowSelected;
     {* The Items property is a TStringList of TBookmarkStr *}
     // property Items[Index: Integer]: TBookmarkStr
     //      read GetItem; default;
//  end;

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, Grids, DBGrids, DB, DBTables;

type
  TForm1 = class(TForm)
    Table1: TTable;
    DBGrid1: TDBGrid;
    Count: TButton;
    Selected: TButton;
    Clear: TButton;
    Delete: TButton;
    Select: TButton;
    GetBookMark: TButton;
    Find: TButton;
    FreeBookmark: TButton;
    DataSource1: TDataSource;
    procedure CountClick(Sender: TObject);
    procedure SelectedClick(Sender: TObject);
    procedure ClearClick(Sender: TObject);
    procedure DeleteClick(Sender: TObject);
    procedure SelectClick(Sender: TObject);
    procedure GetBookMarkClick(Sender: TObject);
    procedure FindClick(Sender: TObject);
    procedure FreeBookmarkClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  Bookmark1: TBookmark;
  z: Integer;

implementation

{$R *.DFM}

//Example of the Count property
procedure TForm1.CountClick(Sender: TObject);
begin
  if DBgrid1.SelectedRows.Count > 0 then
  begin
    showmessage(inttostr(DBgrid1.SelectedRows.Count));
  end;
end;

//Example of the CurrentRowSelected property
procedure TForm1.SelectedClick(Sender: TObject);
begin
  if DBgrid1.SelectedRows.CurrentRowSelected then
    showmessage('Selected');
end;

//Example of the Clear Method
procedure TForm1.ClearClick(Sender: TObject);
begin
  dbgrid1.SelectedRows.Clear;
end;

//Example of the Delete Method
procedure TForm1.DeleteClick(Sender: TObject);
begin
  DBgrid1.SelectedRows.Delete;
end;

{*
   This example iterates through the selected rows of the grid and displays the second field of the dataset.
   The Method DisableControls is used so that the DBGrid will not update when the dataset is changed. The last position of the dataset is saved as a TBookmark.
   The IndexOf method is called to check whether or not the bookmark is still existent.
   The decision of using the IndexOf method rather than the Refresh method should be determined by the specific application.
*}

procedure TForm1.SelectClick(Sender: TObject);
var
  x: word;
  TempBookmark: TBookMark;
begin
  DBGrid1.Datasource.Dataset.DisableControls;
  with DBgrid1.SelectedRows do
  if Count > 0 then
  begin
    TempBookmark:= DBGrid1.Datasource.Dataset.GetBookmark;
    for x:= 0 to Count - 1 do
    begin
      if IndexOf(Items[x]) > -1 then
      begin
        DBGrid1.Datasource.Dataset.Bookmark:= Items[x];
        showmessage(DBGrid1.Datasource.Dataset.Fields[1].AsString);
      end;
    end;
  end;
  DBGrid1.Datasource.Dataset.GotoBookmark(TempBookmark);
  DBGrid1.Datasource.Dataset.FreeBookmark(TempBookmark);
  DBGrid1.Datasource.Dataset.EnableControls;
end;

{*
This example allows you to set a bookmark and  and then search for the bookmarked record within selected a record(s) within the DBGrid.
*}

//Sets a bookmark
procedure TForm1.GetBookMarkClick(Sender: TObject);
begin
  Bookmark1:= DBGrid1.Datasource.Dataset.GetBookmark;
end;

//Frees the bookmark
procedure TForm1.FreeBookmarkClick(Sender: TObject);
begin
  if assigned(Bookmark1) then
  begin
    DBGrid1.Datasource.Dataset.FreeBookmark(Bookmark1);
    Bookmark1:= nil;
  end;
end;

//Uses the Find method to locate the position of the bookmarked record within the selected list in the DBGrid
procedure TForm1.FindClick(Sender: TObject);
begin
  if assigned(Bookmark1) then
  begin
    if DBGrid1.SelectedRows.Find(TBookMarkStr(Bookmark1),z) then
      showmessage(inttostr(z));
  end;
end;

end.

有用(0) 

如何在DBGrid中能支持多项记录的选择的更多相关文章

  1. 如何在MQ中实现支持任意延迟的消息?

    什么是定时消息和延迟消息? 定时消息:Producer 将消息发送到 MQ 服务端,但并不期望这条消息立马投递,而是推迟到在当前时间点之后的某一个时间投递到 Consumer 进行消费,该消息即定时消 ...

  2. 教您如何在Word的mathtype加载项中修改章节号

    在MathType数学公式编辑器中,公式编号共有五部分内容:分别是章编号(Chapter Number).节编号(Section Number).公式编号(Equation Number).括号(En ...

  3. 错误“Sources”参数中指定了多次。“Sources”参数不支持重复项。

    在“Sources”参数中指定了项“”多次.“Sources”参数不支持重复项. Asp.Net关于错误“Sources”参数中指定了多次.“Sources”参数不支持重复项. “Sources”参数 ...

  4. 服务化改造实践 | 如何在 Dubbo 中支持 REST

    什么是 REST REST 是 Roy Thomas Fielding [[1]](#fn1) 在 2000 年他的博士论文 [[2]](#fn2) “架构风格以及基于网络的软件架构设计” 中提出来的 ...

  5. 如何在 WPF 中获取所有已经显式赋过值的依赖项属性

    原文:如何在 WPF 中获取所有已经显式赋过值的依赖项属性 获取 WPF 的依赖项属性的值时,会依照优先级去各个级别获取.这样,无论你什么时候去获取依赖项属性,都至少是有一个有效值的.有什么方法可以获 ...

  6. 如何在 Istio 中支持 Dubbo、Thrift、Redis 以及任何七层协议?

    赵化冰,腾讯云高级工程师,Istio Member,ServiceMesher管理委员,Istio 项目贡献者, Aerika 项目创建者 ,热衷于开源.网络和云计算.目前主要从事服务网格的开源和研发 ...

  7. 如何在VS2013中进行Boost单元测试

    对于如何在VS2013中进行Boost单元测试,这方面资料太少.自己也因此走了不少弯路.下文将会阐述一下如何在VS2013中进行Boost单元测试. 在开始Boost单元测试之前,我们需要先安装VS2 ...

  8. 如何在Ruby中编写微服务?

    [编者按]本文作者为 Pierpaolo Frasa,文章通过详细的案例,介绍了在Ruby中编写微服务时所需注意的方方面面.系国内 ITOM 管理平台 OneAPM 编译呈现. 最近,大家都认为应当采 ...

  9. 如何在Linux中使用Firejail运行应用程序

    有时您可能希望使用在不同环境中未经过良好测试的应用程序,但您必须使用它们.在这种情况下,关注系统的安全性是正常的.在Linux中可以做的一件事是在沙箱中使用应用程序. “沙盒”是在有限环境中运行应用程 ...

随机推荐

  1. java面试资源(面试题、面试经验等)

    两年JAVA程序员的面试总结 https://www.cnblogs.com/xuwujing/p/7613084.html 2018JAVA面试题附答案(长期更新) https://blog.csd ...

  2. 从零搭建HBase集群

    本文从零开始搭建大数据集群,涉及Linux集群安装搭建,Hadoop集群搭建,HBase集群搭建,Java接口封装,对接Java的C#类库封装 Linux集群搭建与配置 Hadoop集群搭建与配置 H ...

  3. 自动化工具 fastmonkey

    Android Monkey 二次开发,实现高速点击的 Android Monkey 一.工具介绍: 1.本工具是testhome上 zhangzhao_lenovo开源出来的工具,源码暂时还未开源: ...

  4. unittest—selenium自动化登录百度绕过校验

    这个脚本融合了unittest的校验,以及selenium的自动化,并且通过派发cookie信息成功绕过百度的验证码,并且利用装饰器成功只打开一次浏览器 #encoding=utf-8 from se ...

  5. [Unity Shader] 坐标变换与法线变换及Unity5新增加的内置函数

    学习第六章Unity内置函数时,由于之前使用mul矩阵乘法时的顺序与书中不一致,导致使用内置函数时出现光照效果不一样,因此引出以下两个问题: 1 什么时候使用3x3矩阵,什么时候使用4x4矩阵? 2 ...

  6. MATLAB复制图片时边框大的问题

    当使用MATLAB画图时,需要将图片复制到word中,会发现图片有一个白色的边框,在论文的排版中是一个影响美观的问题 例如: >> x = 0:10; >> y = sin(x ...

  7. Java SE练习题——求奇数

    欢迎来到Java SE练习题频道,我是Fishing,今天我带来的练习题是(做题会有不足之处,可评论,说出更好的方法): 通过键盘输入两个整数,计算这两个整数之间的所有奇数之和,并输出计算结果. 看到 ...

  8. TensorFlow --- 01初识

    由于博客园对Markdown支持不够友好,阅读此文请前往云栖社区:TensorFlow --- 01初识

  9. Python20 - Day08

    异常处理 一.什么是异常? 异常就是程序运行时发生错误的信号(在程序出现错误时,则会产生一个异常,若程序没有处理他,则会抛出该异常,程序的运行也会停止) 错误分成两种: 1.语法错误 2.逻辑错误 二 ...

  10. GridView的控件说明[字典]-----方便查询

    GridView 控件以表格的形式显示数据,并提供对数据进行排序,选择,编辑,删除等功能. GridView能够完成的功能具体可以总结如下: 1,通过数据源控件将数据绑定到GridView控件 2,对 ...