DBGRID1->Options->dgMultiSelect = true;

解决方案 »

  1.   

    但好象不直接支持SHIFT,大概应该写段代码。
      

  2.   

    在OnCellClick事件中检查Shift的状态,然后对区域中的记录的选择状态取反。
      

  3.   

    //pas
    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
      StdCtrls, Db, DBTables, Grids, DBGrids;type
      TForm1 = class(TForm)
        DBGrid1: TDBGrid;
        Table1: TTable;
        DataSource1: TDataSource;
        Button1: TButton;
        procedure Button1Click(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;var
      Form1: TForm1;implementation{$R *.DFM}procedure TForm1.Button1Click(Sender: TObject);
    begin
      with DBGrid1.DataSource.DataSet do begin
        First;
        while not Eof do begin
          if DBGrid1.SelectedRows.CurrentRowSelected then
            ShowMessage(FieldByName('我不知道你的字段是什么').AsString);
          Next;
        end;
      end;
    end;end.//dfm
    object Form1: TForm1
      Left = 192
      Top = 107
      Width = 544
      Height = 363
      Caption = 'Form1'
      Color = clBtnFace
      Font.Charset = DEFAULT_CHARSET
      Font.Color = clWindowText
      Font.Height = -11
      Font.Name = 'MS Sans Serif'
      Font.Style = []
      OldCreateOrder = False
      PixelsPerInch = 96
      TextHeight = 13
      object DBGrid1: TDBGrid
        Left = 0
        Top = 0
        Width = 536
        Height = 299
        Align = alTop
        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 Button1: TButton
        Left = 216
        Top = 304
        Width = 75
        Height = 25
        Caption = 'Button1'
        TabOrder = 1
        OnClick = Button1Click
      end
      object Table1: TTable
        Active = True
        DatabaseName = 'SY_JXC'
        TableName = '我不知道你的表名是什么.DB'
        Left = 104
        Top = 16
      end
      object DataSource1: TDataSource
        DataSet = Table1
        Left = 56
        Top = 16
      end
    end
    //请按Ctrl吧,要按Shift自己编
      

  4.   

    zswang :ctrl键当然可以,但是不能连选,只能一条一条选择,我要的是如何是通过写代码设置某一条记录为被处于选择状态!
      

  5.   

    //呵!呵!呵!呵!呵!呵!
    //真的要我写出来呀!!
    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
      StdCtrls, Db, DBTables, Grids, DBGrids;type
      TForm1 = class(TForm)
        DBGrid1: TDBGrid;
        DataSource1: TDataSource;
        Table1: TTable;
        procedure DBGrid1KeyDown(Sender: TObject; var Key: Word;
          Shift: TShiftState);
        procedure DBGrid1KeyUp(Sender: TObject; var Key: Word;
          Shift: TShiftState);
        procedure DataSource1DataChange(Sender: TObject; Field: TField);
        procedure DBGrid1MouseUp(Sender: TObject; Button: TMouseButton;
          Shift: TShiftState; X, Y: Integer);
        procedure FormCreate(Sender: TObject);
      private
        { Private declarations }
        FKeyShift: Boolean;
        FRecNo: Integer;
        FOldNo: Integer;
      public
        { Public declarations }
      end;var
      Form1: TForm1;implementation{$R *.DFM}procedure TForm1.DBGrid1KeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    begin
      FKeyShift := ssShift in Shift;
    end;procedure TForm1.DBGrid1KeyUp(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    begin
      FKeyShift := False;
    end;procedure TForm1.DataSource1DataChange(Sender: TObject; Field: TField);
    begin
      FOldNo := FRecNo;
      FRecNo := TDataSource(Sender).DataSet.RecNo;
      if FKeyShift then DBGrid1.SelectedRows.CurrentRowSelected := True;
      FKeyShift := False;
    end;procedure TForm1.DBGrid1MouseUp(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    var
      I: Integer;
    begin
      if FKeyShift then begin
        if -1 in [FOldNo, FRecNo] then Exit;
        if FRecNo > FOldNo then
          for I := FRecNo downto FOldNo do begin
            TDBGrid(Sender).DataSource.DataSet.RecNo := I;
            TDBGrid(Sender).SelectedRows.CurrentRowSelected := True;
          end
        else
          for I := FRecNo to FOldNo do begin
            TDBGrid(Sender).DataSource.DataSet.RecNo := I;
            TDBGrid(Sender).SelectedRows.CurrentRowSelected := True;
          end;
      end;
    end;procedure TForm1.FormCreate(Sender: TObject);
    begin
      FRecNo := -1;
    end;end.
    object Form1: TForm1
      Left = 192
      Top = 107
      Width = 544
      Height = 287
      Caption = 'Form1'
      Color = clBtnFace
      Font.Charset = DEFAULT_CHARSET
      Font.Color = clWindowText
      Font.Height = -11
      Font.Name = 'MS Sans Serif'
      Font.Style = []
      OldCreateOrder = False
      OnCreate = FormCreate
      PixelsPerInch = 96
      TextHeight = 13
      object DBGrid1: TDBGrid
        Left = 0
        Top = 0
        Width = 536
        Height = 260
        Align = alClient
        DataSource = DataSource1
        Options = [dgTitles, dgIndicator, dgColumnResize, dgColLines, dgRowLines, dgTabs, dgRowSelect, dgConfirmDelete, dgCancelOnExit, dgMultiSelect]
        TabOrder = 0
        TitleFont.Charset = DEFAULT_CHARSET
        TitleFont.Color = clWindowText
        TitleFont.Height = -11
        TitleFont.Name = 'MS Sans Serif'
        TitleFont.Style = []
        OnKeyDown = DBGrid1KeyDown
        OnKeyUp = DBGrid1KeyUp
        OnMouseUp = DBGrid1MouseUp
      end
      object DataSource1: TDataSource
        DataSet = Table1
        OnDataChange = DataSource1DataChange
        Left = 112
        Top = 8
      end
      object Table1: TTable
        Active = True
        DatabaseName = 'DBDEMOS'
        TableName = 'biolife.db'
        Left = 144
        Top = 8
      end
    end