Tdbgrid with Multiselect selected with one Click

Asked

Viewed 98 times

1

I’m going through the following problem with a TDBGrid.

That one TDBGrid is with the option dgMultiSelect active. The problem is, I want, with one click the line to be selected as if it had pressed Ctrl + Mouse click and that’s what I don’t know how to do.

  • Code :

    procedure TfrmPrincipal.GridCellClick(Column: TColumn);
    var
      sValorColunaAtivo: Integer;
    begin
      if UpperCase(Column.FieldName) = 'SELECTED' then
      begin
        if DM.FDMemTableExpedicao.FieldByName('SELECTED').AsInteger = 1 then
          sValorColunaAtivo := 0
        else
          sValorColunaAtivo := 1;
        // edita o DataSet, inverte o status e grava os dados
        DM.FDMemTableExpedicao.Edit;
        DM.FDMemTableExpedicao.FieldByName('SELECTED').AsInteger := sValorColunaAtivo;
        DM.FDMemTableExpedicao.Post;
      end;
    end;
    

2 answers

2

An alternative would be to paint the line the user selected through the Ondrawcolumncell event.

procedure TfrmPrincipal.GridDrawColumnCell(Sender: TObject; const Rect: TRect; DataCol: Integer; Column: TColumn;
  State: TGridDrawState);
begin

  if DM.FDMemTableExpedicaoSELECTED.AsInteger = 1 then
  begin
    Grid.canvas.Brush.color := clBlue; // Pinta o fundo da linha de azul
    Grid.Canvas.Font.color := clWhite; // Pinta a fonte de branco
  end;

  Grid.DefaultDrawDataCell(Rect, column.field, state);
end;
  • 1

    good idea plus the issue of counting selected items will still lose.

  • @Why would you lose? This event is only painting the line, you can easily catch the items or count by Grid or Dataset.

  • 1

    I tested only that it does not count how many are selected.

2


Put it on so it’ll work.

  1. You simulate the ctrl + MouseClick.
  2. You develop your Code.
  3. You simulate the ctrl being released.

Code:

procedure TfrmPrincipal.GridCellClick(Column: TColumn);
var
  sValorColunaAtivo: Integer;
begin
  keybd_event(VK_CONTROL,0,0,0); // Mantém pressionada CTRL
  if UpperCase(Column.FieldName) = 'SELECTED' then
  begin
    if DM.FDMemTableExpedicao.FieldByName('SELECTED').AsInteger = 1 then
      sValorColunaAtivo := 0
    else
      sValorColunaAtivo := 1;
    // edita o DataSet, inverte o status e grava os dados
    DM.FDMemTableExpedicao.Edit;
    DM.FDMemTableExpedicao.FieldByName('SELECTED').AsInteger := sValorColunaAtivo;
    DM.FDMemTableExpedicao.Post;
  end;
  keybd_event(VK_CONTROL, $45, KEYEVENTF_EXTENDEDKEY or KEYEVENTF_KEYUP, 0); // Libera (solta) CTRL
end;

That way it stays right.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.