Fill a listview with the properties of a class in delphi

4

I'm doing an MVC application in delphi, it was going okay, when it came to the part of listing the information in a listview, I came across a little problem, as the application is layered, I looped the repeat layer controller, and I was retrieving the information for the view, only returned the last query information, instead of bringing all records, my question is this, how do I display all the information in the view layer:

In the controller layer in the search method:

function TControlEstado.Pesquisar(Pcodigo: string): TControlEstado;
var I : Integer;
begin
  Fcontrole.SqlGeral.Close;
  Fcontrole.SqlGeral.SQL.Clear;
  Fcontrole.SqlGeral.SQL.Add('select est00_codigo      ');
  Fcontrole.SqlGeral.SQL.Add('      ,est00_descri      ');
  Fcontrole.SqlGeral.SQL.Add('      ,est00_uf          ');
  Fcontrole.SqlGeral.SQL.Add('      ,est00_dtCadastro  ');
  Fcontrole.SqlGeral.SQL.Add('      ,est00_status      ');
  Fcontrole.SqlGeral.SQL.Add('from cadest00');
  Fcontrole.SqlGeral.SQL.Add('where est00_descri like ' + QuotedStr('%'+Pcodigo +'%'));
  Fcontrole.SqlGeral.Open;

  if Fcontrole.SqlGeral.IsEmpty then
  begin
    Pcodigo:='';
    ResultadoQuery := 0;
  end;
    ResultadoQuery := 1;
    Fcontrole.SqlGeral.First;
  for I := 0 to Fcontrole.SqlGeral.SQL.Count -1 do
  begin
    while not Fcontrole.SqlGeral.Eof  do
    begin
     Self.est00_codigo         := Fcontrole.SqlGeral.FieldByName('est00_codigo')      .AsInteger  ;
     Self.est00_descri         := Fcontrole.SqlGeral.FieldByName('est00_descri')      .AsString   ;
     Self.est00_uf             := Fcontrole.SqlGeral.FieldByName('est00_uf')          .AsString   ;
     Self.est00_dtCadastro     := Fcontrole.SqlGeral.FieldByName('est00_dtCadastro')  .AsDateTime ;
     Self.est00_status         := Fcontrole.SqlGeral.FieldByName('est00_status')      .AsInteger  ;
     Fcontrole.SqlGeral.Next;
    end;
  end;
end;

In my main form I have created a method that instantiates the connection and class TConttolEstado :

procedure TFrmEstado.Inicialize;
begin
  LcEstoque;                                     //Método que define propriedades host de conexão
  Conectar := TMControlador  .Create;            //Cria a conexão com os parâmetros definidos anteriormente
  Estado   := TControlEstado .Create(Conectar);  // Instância do obejto da classe Estado

end;

// Here I use information retrieval:

procedure TFrmEstado.btnPesquisarClick(Sender: TObject);
begin
  if validaPesquisa then
  begin
    lvGrid.Items.Clear;
    Estado.Pesquisar(edtValor.Text) ;
    if ResultadoQuery = 1 then
    begin
        listItem := lvGrid.Items.Add;
        listItem.Caption:=(IntToStr(Estado.est00_codigo));
        listItem.SubItems.Add(Estado.est00_descri);
        listItem.SubItems.Add(Estado.est00_uf);
        listItem.SubItems.Add(DateToStr(Estado.est00_dtCadastro));
        if Estado.est00_status = 0 then
           listItem.SubItems.Add('Inativo')
        else
        if Estado.est00_status = 1 then
           listItem.SubItems.Add('Ativo');
    end
    else
       MsnAlerta('Erro', 'Vazio');

   lvGrid.Items[0].Selected := True;
  end;
  AjustaForm;
end;

Here's the solution:

In the View layer it looks like this:

procedure TFrmEstado.btnPesquisarClick(Sender: TObject);
var i : Integer;
begin
  if validaPesquisa then
  begin
    lvGrid.Items.Clear;
    Estado.Pesquisar(edtValor.Text, lvGrid) ;
    if ResultadoQuery = 0 then
             MsnAlerta('Erro', 'Vazio')
    else
    begin
      lvGrid.Items[0].Selected := True;
//      Exibir(mImpimir);
    end;
  end;
  AjustaForm;
end;

No controller looks like this:

function TControlEstado.Pesquisar(Pcodigo: string; Lista: TListView): TControlEstado;
   var I : Integer;
  begin
    Fcontrole.SqlGeral.Close;
    Fcontrole.SqlGeral.SQL.Clear;
    Fcontrole.SqlGeral.SQL.Add('select est00_codigo      ');
    Fcontrole.SqlGeral.SQL.Add('      ,est00_descri      ');
    Fcontrole.SqlGeral.SQL.Add('      ,est00_uf          ');
    Fcontrole.SqlGeral.SQL.Add('      ,est00_pais        ');
    Fcontrole.SqlGeral.SQL.Add('from cadest00');
    Fcontrole.SqlGeral.SQL.Add('where est00_descri like ' + QuotedStr('%'+Pcodigo +'%'));
  Fcontrole.SqlGeral.Open;

  if Fcontrole.SqlGeral.IsEmpty then
  begin
    Pcodigo:='';
    ResultadoQuery := 0;
  end
  else
    ResultadoQuery := 1;
    Fcontrole.SqlGeral.First;
  for I := 0 to Fcontrole.SqlGeral.SQL.Count -1 do
  begin
    while not Fcontrole.SqlGeral.Eof  do
    begin
     listaEstado :=Lista.Items.Add;
     listaEstado .Caption   :=(IntToStr(Fcontrole.SqlGeral.FieldByName('est00_codigo').AsInteger));
     listaEstado .SubItems  .Add(Fcontrole.SqlGeral.FieldByName('est00_descri').AsString);
     listaEstado .SubItems  .Add(Fcontrole.SqlGeral.FieldByName('est00_uf').AsString);
     Fcontrole.SqlGeral.Next;
    end;
  end;

end;

Sorry for the delay in responding ...

    
asked by anonymous 24.10.2016 / 20:22

1 answer

1

As far as I can see, you go through all the states and to the last, but as you do not go through again to play in the ListView, so it fills the ListView with the last, because you do not pass the ListView as a parameter in the function search and manipulate the component within the function so no need to worry about traversing again.

Alex, if my answer was able to solve your problem, you could accept it, so it would help the other members of the forum. Hugs!

    
03.11.2016 / 14:05