Solved

DB Grid

Posted on 2000-03-20
8
349 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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 

Author Comment

by:djarmando
ID: 2639663
Fluvio thank you for writing that code for me .. its working !!!
0
 
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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
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…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

820 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