Solved

DB Grid

Posted on 2000-03-20
8
341 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
This video discusses moving either the default database or any database to a new volume.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

757 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

21 Experts available now in Live!

Get 1:1 Help Now