Solved

DB Grid

Posted on 2000-03-20
8
344 Views
Last Modified: 2010-04-04
I select several rows in DB GRID ..and I would like to get their IDs... How
can I do that

for example :
table CUSTOMER
ID_CUSTOMER
NAME

DB GRID IS SHOWING only NAME and then I select several of those customers
but I would like to get their IDs .. how can this be done ??
I'm using delphi 3

10x
Armando


0
Comment
Question by:djarmando
8 Comments
 
LVL 1

Expert Comment

by:fulvio_brasil
ID: 2638810
djarmando try this:

procedure TForm1.BitBtn1Click(Sender: TObject);
var a:integer;
    c: array[1..1000] of integer;
begin
  if DBGrid1.SelectedRows.CurrentRowSelected then
    for a:=0 to DBGrid1.SelectedRows.Count-1 do begin
      Table1.Bookmark := DBGrid1.SelectedRows.Items[a];
      c[a] := Table1.FieldByName('ID_CUSTOMER').Value;
    end;
end;

change the size of array to a best fit for your app.

cheers,

Fulvio
0
 
LVL 2

Expert Comment

by:EdHillmann
ID: 2638869
You should be able to get the value from the dataset, regardless of what is being shown in the grid.  This is assuming that the field is defined in the dataset (if you're using a TTable, then if it's part of the table, it will be).

Typically, the current record in the grid will be the current record in the reflected dataset.  So, to get the ID would be simply referencing its field.

var
vEmpId: string;

....

vEmpId := tblMyTable.FieldByName('ID_CUSTOMER').AsString;

It sounds like you're using multi-select in your grid.  I forget the specific syntax for navigating through a dataset and checking if it's selected.  However, I think the DBGrid contains a property called SelectedRows.  It is a list of bookmarks, with which you can navigate through the dataset with.  Getting the field is the same as above.  Only that you need to go to the appropriate rows defined by  the bookmark list.

Hope this helps,
Ed
0
 

Author Comment

by:djarmando
ID: 2639660
I'm sorry but I would like to get Ids only of selected ones!
0
 

Author Comment

by:djarmando
ID: 2639663
Fluvio thank you for writing that code for me .. its working !!!
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 1

Expert Comment

by:fulvio_brasil
ID: 2640415
Hi Armando,

If it's working, why you reject the answer?

Cheers,

Fulvio.
0
 

Author Comment

by:djarmando
ID: 2640773
I rejected EDs Answer
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 2640981
hi,

here my solution for worth 20 Pts.
d3-code

procedure TForm1.Button1Click(Sender: TObject);
var
  i : integer;
  b : TBookmark;
begin
  Table1.DisableControls;   //avoid visualization and interuption of operation
  b := Table1.GetBookmark;  //mark current record
  listbox1.items.clear;     //could also be a TStringlist
  for i := 0 to dbgrid1.SelectedRows.Count - 1 do //for each selected row
  begin
    Table1.GotoBookmark(TBookmark(dbgrid1.SelectedRows[I]));   //jump to the record
    listbox1.Items.Add(Table1.FieldByName('LackeID').AsString);//get the id
  end;
  Table1.GotoBookmark(B);  //go back to current record
  Table1.FreeBookmark(B);  //free
  Table1.EnableControls;   //user can
end;

meikl
0
 
LVL 1

Accepted Solution

by:
fulvio_brasil earned 20 total points
ID: 2640995
Hi Armando,

I saw that you are a new user... It's the mechanism:

ED send you a comment (so you don't need to accept or reject), I tried to send an answer (so when you accept or reject it's about the proposed answer, not about the comments).

But no problem, just accept this message and i will won the points.

cheers,

Fulvio.
PS: Where are you from?
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

863 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

Need Help in Real-Time?

Connect with top rated Experts

26 Experts available now in Live!

Get 1:1 Help Now