Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

DB Grid

Posted on 2000-03-20
8
Medium Priority
?
358 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Industry Leaders: 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!

 

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 80 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

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 Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses

715 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