?
Solved

Get the text of the selected line in a Dbgrid.

Posted on 2006-05-25
8
Medium Priority
?
297 Views
Last Modified: 2010-04-05
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.
0
Comment
Question by:CodedK
8 Comments
 
LVL 28

Expert Comment

by:2266180
ID: 16759149
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.
0
 
LVL 16

Author Comment

by:CodedK
ID: 16759373
Thanks ciuly.
I dont have the selected row, also i cant imagine how can i get the selected line from mouse event (coords).
0
 
LVL 10

Expert Comment

by:Jacco
ID: 16759475
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
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 16

Author Comment

by:CodedK
ID: 16759533
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."
0
 
LVL 10

Accepted Solution

by:
Jacco earned 1000 total points
ID: 16759627
I added an extra check. The error occored when hovering over the header. I check for lCoord.Y >= 0 now.

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 (lCoord.Y >= 0) and 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
0
 
LVL 16

Author Comment

by:CodedK
ID: 16759783
Thank you very much Jacco and ciuly :)
0
 
LVL 14

Expert Comment

by:Pierre Cornelius
ID: 16759785
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
0
 
LVL 16

Author Comment

by:CodedK
ID: 16759902
Thank you Pierre :)
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
Suggested Courses

807 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question