Marco Web Center

[an error occurred while processing this directive]

Home: Code Repository: Mastering Delphi 5

Project MLTGRID

Project Structure


MLTGRID.DPR

program mltgrid;

uses
  Forms,
  mltdbgrd in 'mltdbgrd.pas' {Form1};

{$R *.RES}

begin
  Application.Initialize;
  Application.CreateForm(TForm1, Form1);
  Application.Run;
end.

MLTDBGRD.PAS

unit mltdbgrd;

interface

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

type
  TForm1 = class(TForm)
    DBGrid1: TDBGrid;
    DataSource1: TDataSource;
    Table1: TTable;
    Splitter1: TSplitter;
    Panel1: TPanel;
    ListBox1: TListBox;
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);
var
  I: Integer;
  BookmarkList: TBookmarkList;
  Bookmark: TBookmarkStr;
begin
  // store the current position
  Bookmark := Table1.Bookmark;
  try
    // empty the listbox
    ListBox1.Items.Clear;
    // get the selected rows of the grid
    BookmarkList := DbGrid1.SelectedRows;
    for I := 0 to BookmarkList.Count - 1 do
    begin
      // for each, move the table to that record
      Table1.Bookmark := BookmarkList[I];
      // add the name field to the listbox
      ListBox1.Items.Add (Table1.FieldByName (
        'Name').AsString);
    end;
  finally
    // go back to the initial record
    Table1.Bookmark := Bookmark;
  end;
end;

end.

MLTDBGRD.DFM

object Form1: TForm1
  Left = 98
  Top = 177
  Width = 563
  Height = 318
  Caption = 'Multiple Selection Grid'
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  OldCreateOrder = True
  PixelsPerInch = 96
  TextHeight = 13
  object Splitter1: TSplitter
    Left = 385
    Top = 0
    Width = 0
    Height = 291
    Cursor = crHSplit
  end
  object DBGrid1: TDBGrid
    Left = 0
    Top = 0
    Width = 385
    Height = 291
    Align = alLeft
    DataSource = DataSource1
    Options = [dgEditing, dgTitles, dgIndicator, dgColumnResize, dgColLines, dgRowLines, dgTabs, dgConfirmDelete, dgCancelOnExit, dgMultiSelect]
    TabOrder = 0
    TitleFont.Charset = DEFAULT_CHARSET
    TitleFont.Color = clWindowText
    TitleFont.Height = -11
    TitleFont.Name = 'MS Sans Serif'
    TitleFont.Style = []
  end
  object Panel1: TPanel
    Left = 385
    Top = 0
    Width = 170
    Height = 291
    Align = alClient
    TabOrder = 1
    object ListBox1: TListBox
      Left = 9
      Top = 40
      Width = 150
      Height = 241
      ItemHeight = 13
      TabOrder = 0
    end
    object Button1: TButton
      Left = 11
      Top = 8
      Width = 150
      Height = 25
      Caption = 'Get Selected'
      TabOrder = 1
      OnClick = Button1Click
    end
  end
  object DataSource1: TDataSource
    DataSet = Table1
    Left = 32
    Top = 24
  end
  object Table1: TTable
    Active = True
    DatabaseName = 'DBDEMOS'
    TableName = 'COUNTRY.DB'
    Left = 88
    Top = 24
  end
end