Solved

Grid & SQL

Posted on 2002-06-28
4
249 Views
Last Modified: 2010-04-04
I have a Grid that has been populated with information retrieved via a SQL query.

I want to be able to multi-select or use a check-box to select only the records I want to use.

When I have chosen my records I then need to find a way of sending only those records selected to a table.

Any ideas??
Thanks
0
Comment
Question by:HotRod40
  • 3
4 Comments
 
LVL 27

Expert Comment

by:kretzschmar
ID: 7115954
thats easy,

sample follows in some hours,
because i have a ready sample,
but just not here

therefore just some hints

set the rowselect and multiselect-options of the gris to true
after selection, iterate through the selected rows

meikl ;-)
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 7116245
here it comes,
sorry its not commented/explained

just ask, if unclear

two grids, where the selected rows are transfered to another table

unit uebertrag_u;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  DBTables, Grids, DBGrids, Db, ExtCtrls, ComCtrls, Buttons, DBCtrls,
  StdCtrls;

type
  TUForm = class(TForm)
    QSource: TQuery;
    DSSource: TDataSource;
    DDest: TDBGrid;
    TDest: TTable;
    DSDest: TDataSource;
    DSource: TDBGrid;
    Sp: TSplitter;
    Panel1: TPanel;
    StatusBar: TStatusBar;
    DBN: TDBNavigator;
    UB: TSpeedButton;
    CB: TComboBox;
    In1: TEdit;
    In2: TEdit;
    Label1: TLabel;
    RR: TSpeedButton;
    SBUP: TSpeedButton;
    SBDN: TSpeedButton;
    TDestTmp: TTable;
    procedure SpMoved(Sender: TObject);
    procedure UBClick(Sender: TObject);
    procedure RRClick(Sender: TObject);
    procedure DSourceKeyPress(Sender: TObject; var Key: Char);
    procedure FormResize(Sender: TObject);
    procedure SBUPClick(Sender: TObject);
    procedure SBDNClick(Sender: TObject);
  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
  end;

var
  UForm: TUForm;

implementation

{$R *.DFM}

procedure TUForm.SpMoved(Sender: TObject);
begin
  If Sp.left - UB.Width > 0 Then
    UB.Left := Sp.left - (UB.Width div 2)
  else
    UB.Left := 0;
end;

procedure TUForm.UBClick(Sender: TObject);
var i,j : Integer;
begin
  if DSource.SelectedRows.Count > 0 then
  begin
    for j := 0 to DSource.SelectedRows.Count - 1 do
    begin
      QSource.GotoBookmark(TBookMark(DSource.SelectedRows.Items[J]));
      TDest.Append;
      For i := 0 to QSource.FieldCount - 1 do
        TDest.Fields[i] := QSource.Fields[I];
      TDest.Post;
    end;
  end;
end;



procedure TUForm.RRClick(Sender: TObject);
const SQLs = 'select name, adrl1, street, zip, city, state from adressen';
var s : String;
begin
  QSource.DisableControls;
  QSource.Active := False;
  QSource.SQL.Clear;
  If CB.Text = '' Then S := ''
  else
    s := ' where '+CB.Text+' between "'+In1.Text+'" and "'+In2.Text+'"';
  QSource.SQL.Add(SQLs+s);
  Try
    QSource.Active := True;
  except
    Showmessage('Eingabefehler ! Startzustand wird wieder hergestellt ! Statement = '+SQLs+S);
    QSource.Active := False;
    QSource.SQL.Clear;
    QSource.SQL.Add(SQLs);
    QSource.Active := True;
  end;
  QSource.EnableControls;
end;

procedure TUForm.DSourceKeyPress(Sender: TObject; var Key: Char);
begin
  If Key = #13 then UBClick(Self);
end;

procedure TUForm.FormResize(Sender: TObject);
begin
  SBUP.Left := DBN.Left - 50;
  SBDN.Left := DBN.Left - 25;
end;

procedure TUForm.SBUPClick(Sender: TObject);
var i : Integer;
begin
  TDestTmp.GotoCurrent(TDest);
  TDestTmp.Prior;
  TDestTmp.Insert;
  For i := 0 to TDest.FieldCount - 1 do
    TDestTmp.Fields[i] := TDest.Fields[I];
  TDest.Delete;
  TDest.GotoCurrent(TDestTmp);
end;

procedure TUForm.SBDNClick(Sender: TObject);
var i : Integer;
begin
  TDestTmp.GotoCurrent(TDest);
  TDestTmp.Next;
  TDestTmp.Next;
  If not TDestTmp.EOf then
  begin
    TDestTmp.Insert;
    For i := 0 to TDest.FieldCount - 1 do
      TDestTmp.Fields[i] := TDest.Fields[I];
    TDest.Delete;
    TDest.GotoCurrent(TDestTmp);
  end;
end;

end.


hope this helps

meikl ;-)
0
 
LVL 27

Accepted Solution

by:
kretzschmar earned 75 total points
ID: 7116403
just to point out what you need, i guess

procedure TUForm.UBClick(Sender: TObject);
var i,j : Integer;
begin
 if DSource.SelectedRows.Count > 0 then
 begin
   for j := 0 to DSource.SelectedRows.Count - 1 do
   begin
     QSource.GotoBookmark(TBookMark(DSource.SelectedRows.Items[J]));
     TDest.Append;
     For i := 0 to QSource.FieldCount - 1 do
       TDest.Fields[i] := QSource.Fields[I];
     TDest.Post;
   end;
 end;
end;


meikl ;-)
0
 

Author Comment

by:HotRod40
ID: 7121806
Thanks but theres one more thing.

I want to be able to update a field but only with the records that have been selected.
How can I do this?

I have tried your example above and it works great with a Select SQL query. I have tried it with an Update SQL query and it says the dataset is not open even though I have actually called the ExecSQL command.

Can I not use an Update within this loop that you have demonstrated for me??

Thanks
Scott
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
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…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

743 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

13 Experts available now in Live!

Get 1:1 Help Now