Solved

Grid & SQL

Posted on 2002-06-28
4
253 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 Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Delphi TcxGrid group footer summary 3 353
Tidtcpserver listening on multiports? 1 41
shape, triangle, dbctrlgrid 3 31
how can i delete one control based on its tagstring ? 6 34
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…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

685 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