Fill a listview with the properties of a class in Delphi

Asked

Viewed 618 times

4

I’m doing an MVC application in Delphi, it was going well, when you arrived at the part of listing the information in a listview, I came across a small problem, as the application is divided into layers, I did the loop of repetition in the controller layer, qnd went to retrieve the information for the view, only returned the last query information, instead of bringing all the 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 created a method that instantiates the connection and the 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 comes the resonation:

The View layer 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;

On the controller went 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 answering...

  • From what I can see, you go through all the states and stop at the last one, but since you don’t come around again to play Listview, so he fills Listview with the last one, because you don’t pass Listview as a parameter in the search function and manipulate the component within the function so you don’t have to worry about going through it again.

  • Good idea, I’ll do it!

  • On second thought, I think this would escape the logic of separating layers... Because the function is part of my controller... You could make an example of how it would look...

  • Thanks Marco Giovanni, it worked like a marvel!

  • managed to make?

  • I got it Yes! the/

  • @Alexdesousa, you can post as the answer?

  • Sorry for the delay in reply: Here is Form View: Procedure Tfrmestado.btnPesquisarClick(Sender: Tobject); var i : Integer; Begin if validaPesquisa then Begin lvGrid.Items.Clear; Status.Search(edtValor.Text, lvGrid) ; if Resultadoquery = 0 then Msnalerta('Error', 'Empty') Else Begin lvGrid.Items[0]. Selected := True; // Display(mImpimir); end; end; Adjustform; end;

Show 3 more comments

1 answer

1


From what I can see, you go through all the states and stop at the last one, but since you don’t come around again to play Listview, so he fills Listview with the last one, because you don’t pass Listview as a parameter in the search function and manipulate the component within the function so you don’t have to worry about going through it again.

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.