Search the database and move to a listview c# Windows Forms

Asked

Viewed 227 times

-2

I am developing a project and for being new in the area I am with enough doubts and difficulties. My problem is the following: I have a form to manage a company’s forward notes, when opening the form, the user needs to enter (or search) the client’s code. And when you type, it will list all the notes that are in this client’s ID. Right?

However, I’m not being able to search for the customer ID. How can I do?

Below is an excerpt of the code to find a customer:

    private void Txt_Busca_TextChanged(object sender, EventArgs e)
    {
        if (txt_Busca.Text.Trim().Equals(String.Empty))
        {
            PreencherLista(lista);
            return;
        }
        else
        {
            var listResult = new List<EntidadeViewPesquisaCliente>(from p in lista where p.Nome.ToLower().Contains(txt_Busca.Text.Trim().ToLower()) select p);
            PreencherLista(listResult);
        }

    }

My code is like this:

using Entities.Entities; using Entities.Sales; using Negocio.Notasng; using project.Modulos; using project.Notes; using project.; using System; using System.Collections.Generic; using System.Componentmodel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms;

project namespace. Sales { public partial class Frmadmnotas : Form { public List list list = new List(); public int iRetorno = 0; public Frmadmnotas() { InitializeComponent(); }

    private void FrmAdmNotas_Load(object sender, EventArgs e)
    {
        var form = new Form()
        {
            FormBorderStyle = FormBorderStyle.None,
            ShowInTaskbar = false,
            StartPosition = FormStartPosition.CenterScreen,
            TopMost = true,
            Top = 0,
            Left = 0
        };

        PrepararLista(lista);
        LimparCampos();
    }
    /// <summary>
    /// Botão para pesquisar o cliente - Abre outro Form e o usuário digita o nome do cliente, buscando pelo código.
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void Bt_PesquisarCliente_Click(object sender, EventArgs e)
    {
        var lista = new NotasNG().ListarEntidadeViewPesquisaCliente();
        //Verifica se a lista está vazia
        if (lista.Count < 1)
        {
            MessageBox.Show("Sem dados para serem exibidos!", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
            return;
        }

        //Passa a lista para o formulário genérico de pesquisa de cliente;

        var frmPesquisaC = new FrmPesquisaCliente("Listagem de Clientes");
        frmPesquisaC.lista = lista;
        frmPesquisaC.ShowDialog();

        var iRetorno = frmPesquisaC.iRetorno;
        //iRetorno = 0
        if (iRetorno < 1)
            return;

        txt_CodigoCliente.Text = iRetorno.ToString();

        Txt_CodigoCliente_Validating(txt_CodigoCliente, new CancelEventArgs());
        bt_PesquisarCliente.Focus();

    }

    private void Txt_CodigoCliente_Validating(object sender, CancelEventArgs e)
    {
        //Verifica se está vazio
        if (txt_CodigoCliente.Text.Trim().Equals(string.Empty))
            return;

        var oCliente = new NotasNG().BuscarCliente(txt_CodigoCliente.Text.Trim());
        //AQUI ESTÁ A DÚVIDA - PRECISO BUSCAR AS NOTAS DO CLIENTE 
        var oNota = new NotasNG().BuscarNotas(Convert.ToInt32(txt_CodigoCliente.Text.Trim()));


        txt_NomeCliente.Text = oCliente.Nome;
        foreach (var item in oNota.NotaLista)
        {
            PreencherListaNota(item);
        }

        Txt_CodigoCliente_TextChanged(txt_CodigoCliente, new EventArgs());


    }

    /// <summary>
    /// Limpa os campos de dados!
    /// </summary>
    private void LimparCampos()
    {
        txt_CodigoCliente.Text = string.Empty;
        iRetorno = 0;
    }

    /// <summary>
    /// Prepara a lista para ser exibida
    /// </summary>
    /// <param name="list"></param>
    public void PrepararLista(List<EntidadeViewPesquisaNota> list)
    {
        lvlListagemNotas.Clear();
        lvlListagemNotas.View = View.Details;
        lvlListagemNotas.Columns.Add("Nº da Nota", 100, HorizontalAlignment.Center);
        lvlListagemNotas.Columns.Add("Nº do Funcionário", 100, HorizontalAlignment.Center);
        lvlListagemNotas.Columns.Add("Nº de Controle", 100, HorizontalAlignment.Center);
        lvlListagemNotas.Columns.Add("Data da Emissão", 150, HorizontalAlignment.Center);
        lvlListagemNotas.Columns.Add("Valor Pago", 100, HorizontalAlignment.Center);
        lvlListagemNotas.Columns.Add("Desconto", 100, HorizontalAlignment.Center);
        lvlListagemNotas.Columns.Add("Data Pagamento", 150, HorizontalAlignment.Left);

        foreach (var item in list)
        {
            var linha = new string[7];
            linha[0] = item.NotaID.ToString();
            linha[1] = item.FuncionarioID.ToString();
            linha[2] = item.NumeroNota;
            linha[3] = item.DataEmissao.ToString();
            linha[4] = item.ValorPago.ToString();
            linha[5] = item.Desconto.ToString();
            linha[6] = item.DataPagamentoFinal.ToString();
            var itmx = new ListViewItem(linha);
            lvlListagemNotas.Items.Add(itmx);
        }
        Funcoes.ListviewColor(lvlListagemNotas);
    }

    /// <summary>
    /// Preenche a lista de Notas com os dados da nota
    /// </summary>
    /// <param name="obj"></param>
    private void PreencherListaNota(Nota obj)
    {

        var linha = new string[7];
        linha[0] = obj.NotaID.ToString();
        linha[1] = obj.FuncionarioID.ToString();
        linha[2] = obj.NumeroNota.ToString();
        linha[3] = obj.DataEmissao.ToString();
        linha[4] = obj.ValorPago.ToString();
        linha[5] = obj.Desconto.ToString();
        linha[6] = obj.DataPagamentoFinal.ToString();

        var itmx = new ListViewItem(linha);
        lvlListagemNotas.Items.Add(itmx);

        Funcoes.ListviewColor(lvlListagemNotas);
    }
    private void LvlListagemNotas_DoubleClick(object sender, EventArgs e)
    {
        Bt_Abrir_Click(bt_Abrir, new EventArgs());

    }


    private void Bt_Abrir_Click(object sender, EventArgs e)
    {

    }



    private void Txt_CodigoCliente_TextChanged(object sender, EventArgs e)
    {
        int ClienteID;
        bool success = Int32.TryParse(txt_CodigoCliente.Text.Trim(), out ClienteID);

        if (success)
        {
            var listResult = new List<EntidadeViewPesquisaNota>(from p in lista where p.ClienteID == ClienteID select p);
            PrepararLista(listResult);
        }
        else
        {
            Console.WriteLine("O texto digitado não é um número inteiro.");
        }


    }

}

}

1 answer

0

Just take the entered value and search the property Id taking into account that the name of your property in the template is even "Id":

      
int Id;
bool success = Int32.TryParse(txt_Busca.Text.Trim(), out Id);

if (success)
{
var listResult = new List<EntidadeViewPesquisaCliente>(from p in lista where p.Id == Id select p); 
PreencherLista(listResult); 
}
else
{
Console.WriteLine("O texto digitado não é um número inteiro.");
}

  • Victor, I followed how you told me, but I didn’t show the intended lines. Could you help me find the error? I edited the question

  • It was just a parenthesis that I typed the extra one, updated the answer, just remove before the error.

Browser other questions tagged

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