Link to home
Start Free TrialLog in
Avatar of CodedK
CodedKFlag for Greece

asked on

Get the text of the selected line in a Dbgrid.

Hi.

Is it possible without creating my own DBGrid component
to get the text of a selected line in Dbgrid ?

Maybe OnMouseMove Event can do this... I've found this code:
http://www.afsoftware.it/Tips/ReadTip.asp?Id=8

To get the text under mouse in RichEdit. Can i use it in DbGrid?

Thanks in advance.
Avatar of 2266180
2266180
Flag of United States of America image

if you have the selected row(s) you can use the example from delphi help:
procedure TForm1.Button1Click(Sender: TObject);
var
  i, j: Integer;
  s: string;
begin
  if DBGrid1.SelectedRows.Count>0 then
    with DBGrid1.DataSource.DataSet do
      for i:=0 to DBGrid1.SelectedRows.Count-1 do
      begin
        GotoBookmark(pointer(DBGrid1.SelectedRows.Items[i]));
        for j := 0 to FieldCount-1 do
        begin

          if (j>0) then s:=s+', ';
          s:=s+Fields[j].AsString;
        end;
        Listbox1.Items.Add(s);
        s:= '';
      end;
end;
if yo don't, then you will have to adapt the example for a custom row (which you can calculate using MouseCoords for example) from the mouse pointer in some onmousexxx event. I don't have any db stuff setup on this machine, so I cannot make an example and test, but you are free to try something out and I'll help you debug/fix whatever doesn't work as expected.
Avatar of CodedK

ASKER

Thanks ciuly.
I dont have the selected row, also i cant imagine how can i get the selected line from mouse event (coords).
This works:

type
  TMyDBGrid = class(TDBGrid)
  end;

procedure TForm1.DBGrid1MouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
var
  lCoord: TGridCoord;
  lsText: string;
  liOldRecord: Integer;
begin
  lsText := '';
  lCoord := DBGrid1.MouseCoord(X, Y);
  if dgIndicator in DBGrid1.Options then
    Dec(lCoord.X);
  if dgTitles in DBGrid1.Options then
    Dec(lCoord.Y);
  if Assigned(TMyDBGrid(DBGrid1).DataLink) then
  begin
    liOldRecord := TMyDBGrid(DBGrid1).DataLink.ActiveRecord;
    try
      TMyDBGrid(DBGrid1).DataLink.ActiveRecord := lCoord.Y;
      if (lCoord.X >= 0) and (lCoord.X < DBGrid1.Columns.Count) and Assigned(DBGrid1.Columns[lCoord.X].Field) then
        lsText := DBGrid1.Columns[lCoord.X].Field.DisplayText;
    finally
      TMyDBGrid(DBGrid1).DataLink.ActiveRecord := liOldRecord;
    end;
  end;
  Label1.Caption := lsText;
end;

Regards Jacco
Avatar of CodedK

ASKER

Thank you Jacco :)

Its working BUT when i right click on the dbgrid (5-6 times)...
I get an access violation ...(there is no event on right click to trigger something)

Project raised exception class EAccessViolation with Message
"Access Violation at address 4DE1D37A in module 'IDDBAS32.DLL'. Read of address 0000006E."
ASKER CERTIFIED SOLUTION
Avatar of Jacco
Jacco
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of CodedK

ASKER

Thank you very much Jacco and ciuly :)
Here's another demo. Same concept as Jacco's but also shows headings, so give the points to him.

PAS FILE:
======
unit Unit1;

interface

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

type
  TMyDBGrid = class(TDBGrid)
  end;

  TForm1 = class(TForm)
    DataSource1: TDataSource;
    Table1: TTable;
    DBGrid1: TDBGrid;
    procedure DBGrid1MouseMove(Sender: TObject; Shift: TShiftState; X,
      Y: Integer);
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.DBGrid1MouseMove(Sender: TObject; Shift: TShiftState; X,
  Y: Integer);
var gc: TGridCoord;
    SaveLink: integer;
begin
  gc:= DBGrid1.MouseCoord(x,y);
  Caption:= Format('(col: %d  row:%d): ', [gc.X, gc.y]);

  SaveLink:= TMyDBGrid(DBGrid1).DataLink.ActiveRecord;
  try
    TMyDBGrid(DBGrid1).DataLink.ActiveRecord:= gc.Y-1;
    if (dgIndicator IN DBGrid1.Options) then (Dec(gc.X));
    if (gc.X >= 0) then
      if (gc.y =0)
        then Caption:= Caption + DBGrid1.Columns.Items[gc.x].Field.DisplayLabel
        else Caption:= Caption + DBGrid1.Columns.Items[gc.x].Field.DisplayText;
  finally
    TMyDBGrid(DBGrid1).DataLink.ActiveRecord:= SaveLink;
  end;
end;

end.


DFM File:
======
object Form1: TForm1
  Left = 192
  Top = 114
  Width = 696
  Height = 480
  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 = 688
    Height = 446
    Align = alClient
    DataSource = DataSource1
    TabOrder = 0
    TitleFont.Charset = DEFAULT_CHARSET
    TitleFont.Color = clWindowText
    TitleFont.Height = -11
    TitleFont.Name = 'MS Sans Serif'
    TitleFont.Style = []
    OnMouseMove = DBGrid1MouseMove
  end
  object DataSource1: TDataSource
    DataSet = Table1
    Left = 104
    Top = 48
  end
  object Table1: TTable
    Active = True
    DatabaseName = 'DBDEMOS'
    TableName = 'employee.db'
    Left = 176
    Top = 48
  end
end

Regards
Pierre
Avatar of CodedK

ASKER

Thank you Pierre :)