Lazarus

Programming => Databases => Topic started by: rkamarowski on June 03, 2008, 04:50:34 pm

Title: DBGrid columns visible/not visible
Post by: rkamarowski on June 03, 2008, 04:50:34 pm
I'm trying to reuse a single DBGrid for multiple tables.  I have a list box, and when an entry is selected all columns are made invisible, and only the selected table entries are made visible.  It works fine the first time the entry is clicked.  The second time the entry is clicked all the fields show up.

Example in the following code:

Chemical is clicked in the list box.  The CHEMICALID field is invisible and the CHEMICAL field is visible with the title caption Chemical.  I click on another entry and nothing happens (which is fine at the moment).  If I click Chemical again, both the CHEMICALID and CHEMICAL field are visible, and the CHEMICAL title caption is no longer Chemical, but CHEMICAL.

Code: [Select]
procedure TFormMain.ListBoxTablesClick(Sender: TObject);
var
   i: integer;
   index: integer;
begin
  index:=ListBoxTables.ItemIndex;
  if index<0 then exit;

  // make all columns not visible
  for i:=0 to DBGridGeneric.Columns.Count-1 do
    DBGridGeneric.Columns[i].Visible:=False;
  //DBGridGeneric.Clear;
  // chemical table
  if ListBoxTables.Items[index]='Chemical' then
    begin
      ZTableGeneric.TableName:='CHEMICAL';
      ZTableGeneric.SortedFields:='CHEMICAL';
      ZTableGeneric.Active:=True;
      with DBGridGeneric do
        begin
          for i:=0 to Columns.Count-1 do
            if Columns[i].Title.Caption='CHEMICALID' then
              Columns[i].Visible:=False;
            if Columns[i].Title.Caption='CHEMICAL' then
              begin
                Columns[i].Title.Caption:='Chemical';
                Columns[i].Visible:=True;
              end;
           showmessage(booltostr(columns[0].Visible));
        end;
    end; // chemical table
{
  // measure units table
  if ListBoxTables.Items[index]='Measure Units' then
  showmessage('yup');

    begin
      with DBGridGeneric do
        begin
          ZTableGeneric.TableName:='MEASURE UNITS';
          ZTableGeneric.SortedFields:='UNITS';
          for i:=0 to Columns.Count-1 do
            if Columns[i].Title.Caption='UNITS' then
              begin
                Columns[i].Title.Caption:='Unit';
                Columns[i].Visible:=True;
              end;
         end;
     end; // measure units
 }
end;


What am I doing wrong?

bob k.
Title: RE: DBGrid columns visible/not visible
Post by: OnixJr on June 04, 2008, 12:44:21 am
Can you upload a sample program?

Actually, I'm using a routine that close dataset, clear columns, create new columns and open the dataset again...

Here a piece of the code:
Code: [Select]

procedure TfrRelatorios.ViewReport;
  procedure NewColumn(Col: TColumn; FN: String; Cap: TCaption; Wid: Integer);
  begin
    with Col do
    begin
      FieldName := FN;
      Width := Wid;
      Title.Caption := Cap;
    end;
  end;
var
  I: Byte;
  stSQL: String;
begin
  dmModulo.SQLdsRelatorio.Close;

  case rgTipoRelatorio.ItemIndex of
    0:
      begin
        with dmModulo.SQLdsRelatorio do
        begin
          stSQL := 'SELECT %s FROM produtos;';
          SQL := Format(stSQL, ['produto, SUM(preco), SUM(qtde)']);
          Open;
          {..}
          Close;

          // Start TDBGrid
          SQL := Format(stSQL, ['*']);
          with dbgRelatorio do
          begin
            with Columns do
            begin
              Clear;
              for I := 0 to 4 do
                Columns.Add;
              NewColumn(TColumn(Columns[0]), 'codigo', 'Código', 50);
              NewColumn(TColumn(Columns[1]), 'produto', 'Produto', 450);
              NewColumn(TColumn(Columns[2]), 'preco', 'Preço', 55);
              NewColumn(TColumn(Columns[3]), 'vendas', 'Vendas', 50);
              NewColumn(TColumn(Columns[4]), 'qtde', 'Quantidade', 60);
            end;
          end;
        end;
      end;
    1:
      begin
        stSQL := 'SELECT %s FROM vendas ';
        {...}

        // Start TDBGrid
        dmModulo.SQLdsRelatorio.SQL := Format(stSQL, ['*']);
        with dbgRelatorio do
        begin
          with Columns do
          begin
            Clear;
            for I := 0 to 5 do
              Columns.Add;
            NewColumn(TColumn(Columns[0]), 'codigo', 'Código', 50);
            NewColumn(TColumn(Columns[1]), 'produto', 'Produto', 400);
            NewColumn(TColumn(Columns[2]), 'interno', 'Interno', 50);
            NewColumn(TColumn(Columns[3]), 'preco', 'Preço', 55);
            NewColumn(TColumn(Columns[4]), 'data', 'Data', 75);
            NewColumn(TColumn(Columns[5]), 'hora', 'Hora', 35);
          end;
        end;
      end;
  end;

  dmModulo.SQLdsRelatorio.Open;
end;


Regards,
Júnior
Title: DBGrid columns visible/not visible
Post by: rkamarowski on June 04, 2008, 03:00:51 pm
Junior,

How do I upload the zip file?

bob k.
Title: DBGrid columns visible/not visible
Post by: OnixJr on June 04, 2008, 05:46:05 pm
Try http://www.uploading.com/

Regards,
Júnior
Title: DBGrid columns visible/not visible
Post by: rkamarowski on June 04, 2008, 06:06:54 pm
Junior,

Here's the link:

http://www.uploading.com/files/EMZHVH5Q/Lazarus.zip.html
TinyPortal © 2005-2018