Delphi Pages Forums  

Go Back   Delphi Pages Forums > Delphi Forum > DB-Aware

Lost Password?

Reply
 
Thread Tools Display Modes
  #1  
Old 06-25-2003, 08:11 AM
nonumber nonumber is offline
Member
 
Join Date: May 2003
Posts: 41
Default Selected Rows in DBGrid

I currently have a DBGrid that is populated by a SQL dataset and I have it set as a multi select grid. I would like to know how to read in specific column values of all selected rows in the DBGrid.

e.g. My DbGrid would show a list of checks with their appropriate attributes. FOR EACH selected row, I want to be able to read in the check number and run an update statement on the selected checks.
Reply With Quote
  #2  
Old 06-25-2003, 09:04 AM
MrBaseball34 MrBaseball34 is offline
Senior Member
 
Join Date: Jan 2001
Posts: 7,260
Default RE: Selected Rows in DBGrid

Try this: <cb>{Modified from the example in the help file}</cb>
<tt>
var
i, j: Integer;
s: string;
slCheckNums: TStringList;
begin
slCheckNums := TStringList.Create;
try
if DBGrid1.SelectedRows.Count>0 then
begin
with DBGrid1.DataSource.DataSet do
begin
for i:=0 to DBGrid1.SelectedRows.Count-1 do
begin
GotoBookmark(pointer(DBGrid1.SelectedRows.Items[i]));
slCheckNums.Items.Add(s);
s:= '';
end; {for}
end; {with}
end; {while}
finally
// do whatever with your list of check numbers
slCheckNums.Free;
end; {try..finally}
end;</tt>

Confusious say:
Never stand between fire hydrant and dog.

MB34
Reply With Quote
  #3  
Old 06-25-2003, 02:11 PM
GeoWink GeoWink is offline
Senior Member
 
Join Date: Jan 2001
Posts: 885
Default RE: Selected Rows in DBGrid

I have a grid set up by a query, too. I use a button to set up the grid for individual selection first...

with VBSDBGrid do
begin
if not (msoAutoUnselect in MultiSelectOptions) then
MultiSelectOptions := MultiSelectOptions + [msoAutoUnselect];
if not (msoShiftSelect in MultiSelectOptions) then
MultiSelectOptions := MultiSelectOptions + [msoShiftSelect];
if not (dgMultiSelect in Options) then
Options := Options + [dgMultiSelect];
end;

You can then select the rows you want by using the CTRL key. Then I run the following code.

with VBSQuery do
begin
Close;
SQL.Clear;
SQL.ADD('select * from VBSReg T');
SQL.ADD('where (T."TheYear"= '''+IntToStr(DD)+''')');
DisableControls;
Open;
EnableControls;
CountLabel.Caption := 'There are ' + IntToStr(RecordCount) + ' records';
end;
SCount := 0;
with VBSDBGrid, VBSDBGrid.Datasource.Dataset do
begin
for I := 0 to SelectedList.Count-1 do
begin
GoToBookmark(SelectedList.Items[I]);
SCount := SCount + 1;
// GET THE COLUMN DATA YOU WANT TO WORK WITH
TNIND[SCount,1] := VBSQuery.FieldByName('FirstName').asString;
TNIND[SCount,2] := VBSQuery.FieldByName('LastName').asString;
TNIND[SCount,3] := VBSQuery.FieldByName('Add1').asString;
TNIND[SCount,4] := VBSQuery.FieldByName('Add2').asString;
TNIND[SCount,5] := VBSQuery.FieldByName('City').asString;
TNIND[SCount,6] := VBSQuery.FieldByName('State').asString;
TNIND[SCount,7] := VBSQuery.FieldByName('Zip').asString;
TNIND[SCount,8] := VBSQuery.FieldByName('Phone').asString;
TNIND[SCount,9] := VBSQuery.FieldByName('HomeChurch').asString;
end;
end;
// DO WHATEVER YOU WANT TO DO HERE
with VBSDBGrid do
begin
MultiSelectOptions := MultiSelectOptions - [msoAutoUnselect];
MultiSelectOptions := MultiSelectOptions - [msoShiftSelect];
Options := Options - [dgMultiSelect];
end;

Hope this helps.

George
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 08:55 AM.


Powered by vBulletin® Version 3.8.8
Copyright ©2000 - 2019, vBulletin Solutions, Inc.