上的Shift键选择多行+单击DBGrid中

问题描述 投票:0回答:3

我需要编写代码来选择上的Shift +鼠标点击多行,让我能够向下填充值。有没有什么办法可以做到这一点?我DBGrid的选项如下:

 dbGrid1.Options = [dgEditing, dgAlwaysShowEditor, dgTitles, dgColLines, dgRowLines, dgTabs, dgConfirmDelete, dgCancelOnExit, dgMultiSelect]
delphi delphi-7 dbgrid tdbgrid
3个回答
3
投票

我原本以为有问题,你很可能会与这是Shift键+单击由电网本身处理,所以时间网格的OnMouseUp火灾,电网已经移动数据集光标,这是移位单击的行。所以,延长从那里是前移位点击不会是简单的,因为你缺乏在DS光标以前是信息网格的选择。

然而,事实证明是非常简单的事情。我已经添加到下面的代码的一些注释来解释它是如何工作的。

为了简单起见,我已经使用具有一个整数ID字段中的ClientDataSet实现它。使用ID字段的一点是,代码使用ID的当前和先前值,在一个类的交接手方式作为数据集卷轴,来完成其东西。否则使用书签类似的将是混乱,因为需要不断地分配和释放他们,所以我将它作为一个练习留给读者。

码:

//  Form variables
  SettingBookmarkRange : Boolean;
  CurrentRowID,
  PreviousRowID: Integer;

procedure TForm1.CDS1AfterScroll(DataSet: TDataSet);
begin
  if SettingBookmarkRange then exit;
  PreviousRowID := CurrentRowID;
  CurrentRowID := CDS1.FieldByName('ID').AsInteger;
  Caption := Format('Current %d, prior %d', [CurrentRowID, PreviousRowID]);
end;

procedure TForm1.SetBookmarkRange;
var
  BM,
  TempBM : TBookmark;
  NewBM : TBookmarkStr;
  FoundPrevious : Boolean;
begin

  //  This code is called after the user does a Shift-Click in the grid
  //  First we set a flag to temporarily prevent the CurrrentRowID and
  //  PreviousrowID from being updated during the dataset's OnScroll event
  SettingBookmarkRange := True;

  BM := CDS1.GetBookmark;

  //  Set a flag to keep track of whether we've found the row with the PreviousRowID
  FoundPrevious := False;
  try
    CDS1.DisableControls;

    //  First, search forwards to see if we can find the the row with the PreviousRowID
    //  In other words, we're first checking that the Shift-Click was on a row *above*
    //  the previous row
    CDS1.Next;
    while not FoundPrevious and not CDS1.Eof do begin
       if CDS1.FieldByName('ID').AsInteger = PreviousRowID then begin
         FoundPrevious := True;

         //  We found the row with the PreviousRowID, so now get the Grid to add it, and
         //  all the rows back to where we started, in its SelectedRows list
         while not CDS1.Bof and (CDS1.FieldByName('ID').AsInteger <> CurrentRowID) do begin
           DBGrid1.SelectedRows.CurrentRowSelected := True;
           CDS1.Prior;
         end;
      end
       else
         CDS1.Next;
    end;
    if not FoundPrevious then begin
      //  If we get here, the Shift-Click must have been in a row further down the
      //  grid than the previously-current one
      while not FoundPrevious and not CDS1.Bof do begin
        if CDS1.FieldByName('ID').AsInteger = PreviousRowID then begin
          FoundPrevious := True;
         //  We found the row with the PreviousRowID, so now get the Grid to add it, and
         //  all the rows back to where we started, in its SelectedRows list
          while not CDS1.Eof and (CDS1.FieldByName('ID').AsInteger <> CurrentRowID) do begin
            DBGrid1.SelectedRows.CurrentRowSelected := True;
            CDS1.Next;
          end;
        end
        else
          CDS1.Prior;
      end;
    end;
  finally
    CDS1.GotoBookmark(BM);
    CDS1.FreeBookmark(BM);
    CDS1.EnableControls;
    SettingBookmarkRange := False;
  end;
end;

procedure TForm1.DBGrid1MouseUp(Sender: TObject; Button: TMouseButton; Shift:
    TShiftState; X, Y: Integer);
begin
  Caption := IntToStr(CDS1.Fields[0].Value);
  if ssShift in Shift then
    SetBookMarkRange;
end;

0
投票

为多选网格默认选择行为是按住Ctrl单击,不按住Shift键单击。使用它不需要任何特定的鼠标点击处理程序。

为了实现按住Shift键单击代替,你有推翻/实现电网的鼠标点击处理程序。


0
投票

我用一个TDBGrid class中介。它的工作原理与按Ctrl + Shift键:

type
  TDBGrid = class(Vcl.DBGrids.TDBGrid)
  private
    procedure SelectRange;
  protected
    procedure MouseDown(Button: TMouseButton; Shift: TShiftState; X: Integer; Y: Integer); override;
  end;

  {...}

implementation

  {...}
procedure TDBGrid.SelectRange;
var
  CurrentBookMark, CursorBookMark, FirstBookMark, LastBookMark: TBookmark;
  Dir: integer;
begin
  if SelectedRows.Count <= 1 then
    exit;

  DataSource.DataSet.DisableControls;
  try
    FirstBookMark := SelectedRows.Items[0];
    LastBookMark := SelectedRows.Items[SelectedRows.Count - 1];
    SelectedRows.Clear;
    CurrentBookMark := DataSource.DataSet.GetBookmark;
    Dir := DataSource.DataSet.CompareBookmarks(FirstBookMark, CurrentBookMark);

    if Dir = 0 then
      Dir := DataSource.DataSet.CompareBookmarks(LastBookMark, CurrentBookMark);

    if Dir > 0 then
      DataSource.DataSet.GotoBookmark(LastBookMark)
    else if Dir < 0 then
      DataSource.DataSet.GotoBookmark(FirstBookMark)
    else
      Exit;

    while not DataSource.DataSet.eof do
    begin
      CursorBookMark := DataSource.DataSet.GetBookmark;
      SelectedRows.CurrentRowSelected := true;

      if DataSource.DataSet.CompareBookmarks(CurrentBookMark, CursorBookMark) = 0 then
      begin
        DataSource.DataSet.FreeBookMark(CursorBookMark);
        break;
      end;
      DataSource.DataSet.FreeBookMark(CursorBookMark);

      if Dir < 0 then
        DataSource.DataSet.Next
      else
        DataSource.DataSet.Prior;
    end;

    DataSource.DataSet.FreeBookMark(CurrentBookMark);
  finally
    DataSource.DataSet.EnableControls;
  end;
end;

{ TDBGrid }


procedure TDBGrid.MouseDown(Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  inherited;
  if Shift = [ssCtrl, ssShift, ssLeft] then
    SelectRange;
end;
© www.soinside.com 2019 - 2024. All rights reserved.