DB Grid

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


djarmandoAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
fulvio_brasilConnect With a Mentor Commented:
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
 
fulvio_brasilCommented:
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
 
EdHillmannCommented:
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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
djarmandoAuthor Commented:
I'm sorry but I would like to get Ids only of selected ones!
0
 
djarmandoAuthor Commented:
Fluvio thank you for writing that code for me .. its working !!!
0
 
fulvio_brasilCommented:
Hi Armando,

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

Cheers,

Fulvio.
0
 
djarmandoAuthor Commented:
I rejected EDs Answer
0
 
kretzschmarCommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.