CodedK
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.
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.
ASKER
Thanks ciuly.
I dont have the selected row, also i cant imagine how can i get the selected line from mouse event (coords).
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(Se nder: 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).DataLin k.ActiveRe cord;
try
TMyDBGrid(DBGrid1).DataLin k.ActiveRe cord := lCoord.Y;
if (lCoord.X >= 0) and (lCoord.X < DBGrid1.Columns.Count) and Assigned(DBGrid1.Columns[l Coord.X].F ield) then
lsText := DBGrid1.Columns[lCoord.X]. Field.Disp layText;
finally
TMyDBGrid(DBGrid1).DataLin k.ActiveRe cord := liOldRecord;
end;
end;
Label1.Caption := lsText;
end;
Regards Jacco
type
TMyDBGrid = class(TDBGrid)
end;
procedure TForm1.DBGrid1MouseMove(Se
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
begin
liOldRecord := TMyDBGrid(DBGrid1).DataLin
try
TMyDBGrid(DBGrid1).DataLin
if (lCoord.X >= 0) and (lCoord.X < DBGrid1.Columns.Count) and Assigned(DBGrid1.Columns[l
lsText := DBGrid1.Columns[lCoord.X].
finally
TMyDBGrid(DBGrid1).DataLin
end;
end;
Label1.Caption := lsText;
end;
Regards Jacco
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."
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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(Se nder: 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).DataLin k.ActiveRe cord;
try
TMyDBGrid(DBGrid1).DataLin k.ActiveRe cord:= 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.Di splayLabel
else Caption:= Caption + DBGrid1.Columns.Items[gc.x ].Field.Di splayText;
finally
TMyDBGrid(DBGrid1).DataLin k.ActiveRe cord:= 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
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(Se
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).DataLin
try
TMyDBGrid(DBGrid1).DataLin
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
else Caption:= Caption + DBGrid1.Columns.Items[gc.x
finally
TMyDBGrid(DBGrid1).DataLin
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
ASKER
Thank you Pierre :)
procedure TForm1.Button1Click(Sender
var
i, j: Integer;
s: string;
begin
if DBGrid1.SelectedRows.Count
with DBGrid1.DataSource.DataSet
for i:=0 to DBGrid1.SelectedRows.Count
begin
GotoBookmark(pointer(DBGri
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.