Icon View Thread

The following is the text of the current message along with any replies.
Messages 1 to 6 of 6 total
Thread Fill StringGrid with data from query result
Tue, Mar 4 2008 6:15 PMPermanent Link

Peter van Mierlo
hi,

I have TStringGrid which has to be filled with data from a query. Based on a value, which is 0 or 1, the stringgrid must be filled with data from
query 1 OR qruery 2. Both queries are based on the same database and containing the same 6 fields including 2 lookup fields (showItemName,showItemCount.).
The stringgrid must only show the data from the lookup fields. When using the code below it will show me all 6 fields. But how can i fix the code that it
only show the data from those 2 lookup fields

procedure TFormUitgifte.UitgifteItems;
var
 I, J: Integer;
 AantalF, AantalR : Integer;
begin
  StringGrid.FixedCols := 0;
  StringGrid.FixedRows := 0;
  AantalF := dmTables.qry_itemBYuitgifte.FieldCount;
     AantalR := dmTables.qry_itemBYuitgifte.RecordCount;
     StringGrid.ColCount := AantalF;
     StringGrid.RowCount := AantalR;
     for I := 0 to pred(AantalF) do begin
        StringGrid.Cells[I, 0] := dmTables.qry_itemBYuitgifte.Fields[i].FieldName;
     end;
     J := 1;
     while not dmTables.qry_itemBYuitgifte.Eof do begin
       for I := 0 to pred(AantalF) do
         StringGrid.Cells[I, J] := dmTables.qry_itemBYuitgifte.Fields[i].AsString;
         dmTables.qry_itemBYuitgifte.Next;
         Inc(J);
     end;
end;
Tue, Mar 4 2008 6:44 PMPermanent Link

"Robert"

"Peter van Mierlo" <p.mierlo@planet.nl> wrote in message
news:9CD8D692-63F2-47E3-A3BB-0A21B81F36BD@news.elevatesoft.com...
> The stringgrid must only show the data from the lookup fields. When using
> the code below it will show me all 6 fields. But how can i fix the code
> that it
> only show the data from those 2 lookup fields
>

Why not use 2 instead of fieldcount? and then simply test for field name to
select only the fields that you want.

> procedure TFormUitgifte.UitgifteItems;
function ColumnWanted(const c : string) : boolean;
begin
 result := (c = 'fielda') or (c = 'fieldb');
end;
> var
>  I, J, MyColumn: Integer;
>  AantalF, AantalR : Integer;
> begin
>   StringGrid.FixedCols := 0;
>   StringGrid.FixedRows := 0;
>   AantalF := dmTables.qry_itemBYuitgifte.FieldCount;
>      AantalR := dmTables.qry_itemBYuitgifte.RecordCount;
>      StringGrid.ColCount := AantalF;
>      StringGrid.RowCount := AantalR;
      MyColumn := -1;
>      for I := 0 to pred(AantalF) do begin
         if ColumnWanted(dmTables.qry_itemBYuitgifte.Fields[i].FieldName)
then begin
         inc(MyColumn);
>        StringGrid.Cells[MyColumn, 0] :=
> dmTables.qry_itemBYuitgifte.Fields[i].FieldName;
       end;


Then do the same when you move the fields.

Curious. Why use a stringGrid to display a query?

Robert

>      end;
>      J := 1;
>      while not dmTables.qry_itemBYuitgifte.Eof do begin
>        for I := 0 to pred(AantalF) do
>          StringGrid.Cells[I, J] :=
> dmTables.qry_itemBYuitgifte.Fields[i].AsString;
>          dmTables.qry_itemBYuitgifte.Next;
>          Inc(J);
>      end;
> end;
>

Wed, Mar 5 2008 4:04 AMPermanent Link

"Rita"

A generic procedure to do the job.
Just modify to suit your needs.
Rita

Procedure QueryGrid(DBName, Statement: String; Target: TStringGrid; Titles:
Boolean);
Var Col,  Lin:  Integer;
begin
 With TQuery.Create(Nil) Do
 Try
   DatabaseName := DBName;
   SQL.Text := Statement;
   Open;
   If Not IsEmpty Then
   Begin
     Target.ColCount := FieldCount;
     Target.RowCount := RecordCount+ IIF(Titles,1,0);
     Target.FixedCols := 0;
     Target.FixedRows := IIF(Titles,1,0);
     If Titles Then
       For Col := 0 To FieldCount-1 Do
         Target.Cells[Col,0] := Fields[Col].FieldName;
     Lin := 0;
     While Not Eof Do
     Begin
       For Col := 0 To FieldCount-1 Do
         Target.Cells[Col,Lin+Target.FixedRows] :=
                Fields[Col].AsString;
       Next;
       Inc(Lin);
     End;
   End;
 Finally
   Close;
   Free;
 End;
end; { QueryGrid }

Wed, Mar 5 2008 7:33 AMPermanent Link

Peter van mierlo
hi Robert

i'm goint to try your and Rita's suggestions. Why using a stringgrid ?
well.. if have to display data which can be in query 1 or query 2, based
on a value. A other solution would be using a dbgrid and swiching datasource,
but I don't have any experiance if this could create any problems.

when a user scans a tickets the guests info and additional info about
certain attributes he/she gets like a t-shirt, wristband, food and drink
tickets has to be showed in the grid...it has to work fast.

because of using 2 queries which can contain this info, i thought the best
way is to use a stringgrid...but other suggestions are welcome.

Peter








"Robert" <ngsemail2005withoutthis@yahoo.com.ar> wrote:


"Peter van Mierlo" <p.mierlo@planet.nl> wrote in message
news:9CD8D692-63F2-47E3-A3BB-0A21B81F36BD@news.elevatesoft.com...
> The stringgrid must only show the data from the lookup fields. When using
> the code below it will show me all 6 fields. But how can i fix the code
> that it
> only show the data from those 2 lookup fields
>

Why not use 2 instead of fieldcount? and then simply test for field name to
select only the fields that you want.

> procedure TFormUitgifte.UitgifteItems;
function ColumnWanted(const c : string) : boolean;
begin
 result := (c = 'fielda') or (c = 'fieldb');
end;
> var
>  I, J, MyColumn: Integer;
>  AantalF, AantalR : Integer;
> begin
>   StringGrid.FixedCols := 0;
>   StringGrid.FixedRows := 0;
>   AantalF := dmTables.qry_itemBYuitgifte.FieldCount;
>      AantalR := dmTables.qry_itemBYuitgifte.RecordCount;
>      StringGrid.ColCount := AantalF;
>      StringGrid.RowCount := AantalR;
      MyColumn := -1;
>      for I := 0 to pred(AantalF) do begin
         if ColumnWanted(dmTables.qry_itemBYuitgifte.Fields[i].FieldName)
then begin
         inc(MyColumn);
>        StringGrid.Cells[MyColumn, 0] :=
> dmTables.qry_itemBYuitgifte.Fields[i].FieldName;
       end;


Then do the same when you move the fields.

Curious. Why use a stringGrid to display a query?

Robert

>      end;
>      J := 1;
>      while not dmTables.qry_itemBYuitgifte.Eof do begin
>        for I := 0 to pred(AantalF) do
>          StringGrid.Cells[I, J] :=
> dmTables.qry_itemBYuitgifte.Fields[i].AsString;
>          dmTables.qry_itemBYuitgifte.Next;
>          Inc(J);
>      end;
> end;
>

Wed, Mar 5 2008 8:39 AMPermanent Link

Roy Lambert

NLH Associates

Team Elevate Team Elevate

Peter

Flipping the datasource is a doddle - one line of code. The big problem is with flicker for the UI. Almost all my grids are"preformatted" with the columns, headers (titles) and widths I want. If that's the case here I might think of selecting into a memory table and having that linked to the grid. You run either query1 or query2 depending on your test, disable the grid before you run the query and enable it afterwards to help minimise flicker.


Roy Lambert
Wed, Mar 5 2008 8:57 AMPermanent Link

"Robert"

"Peter van mierlo" <p.mierlo@planet.nl> wrote in message
news:B0E32CAD-3198-4A38-97B5-D393338BDAA8@news.elevatesoft.com...
> hi Robert
>
> i'm goint to try your and Rita's suggestions. Why using a stringgrid ?
> well.. if have to display data which can be in query 1 or query 2, based
> on a value. A other solution would be using a dbgrid and swiching
> datasource,
> but I don't have any experiance if this could create any problems.
>

You don't even need to change datasource. Just change the dataset name on
the datasource before you open the query. Better yet (and cleaner design,
IMO) would be to have just one query, and build the SQL on the fly depending
on what you want.

Robert

>

Image