Error loading data from a listview into a texbox c# Windows Forms

Asked

Viewed 63 times

0

inserir a descrição da imagem aqui

I am developing my project in Windows forms, where in the register tab shows a form that has a listview with some customer data, and next several texbox to register, change, view or delete this client. I already have some registered customers, I can see on listbox. However, when double-clicking on the line, the system needed to take the ID of that line (which is a listview item), pass it to the database and upload all the information from that codeID. Would it be possible? how to do?

So far I managed to upload the data by the full name of the client, however, to facilitate, the ideal would be to double-click the desired line.

My Database Class:

   public Cliente Buscar(string nome)
        {
            Cliente oCliente = new Cliente();
            using (MySqlConnection conexao = ConexaoBD.getInstancia().getConexao())
            {
                try
                {
                    conexao.Open();
                    MySqlCommand comando = new MySqlCommand();
                    comando = conexao.CreateCommand();


                    comando.CommandText = "select * from cliente where nome  = @nome;";
                    comando.Parameters.AddWithValue("nome", nome).ToString();
                    MySqlDataReader reader = comando.ExecuteReader();
                    while (reader.Read())
                    {
                        //Objeto Cliente
                        oCliente.ClienteID = Convert.ToInt32(reader["ClienteID"].ToString());
                        oCliente.Nome = reader["Nome"].ToString();
                        oCliente.CpfCnpj = reader["CpfCnpj"].ToString();
                        oCliente.Telefone = reader["Telefone"].ToString();
                        oCliente.Rua = reader["Rua"].ToString();
                        oCliente.Numero = reader["Numero"].ToString();
                        oCliente.Bairro = reader["Bairro"].ToString();
                        oCliente.Cidade = reader["Cidade"].ToString();
                        if (reader["Observacao"] != null)
                            oCliente.Observacao = reader["Observacao"].ToString();
                        oCliente.Limite = reader["Limite"].ToString();
                        oCliente.UsuarioID = Convert.ToInt32(reader["UsuarioID"].ToString());
                        oCliente.DataAlteracao = Convert.ToDateTime(reader["DataAltercao"].ToString());
                        if (reader["Complemento"] != null)
                            oCliente.Complemento = reader["Complemento"].ToString();
                    }
                }
                catch (MySqlException ex)
                {
                    throw new System.Exception(ex.ToString());
                }
                finally
                {
                    conexao.Close();
                }
            }
            return oCliente;
        }

and calling on Forms

private void Txt_NomeCliente_Validating_1(object sender, CancelEventArgs e)
    {
        //Verifica se está vazio
        if (txt_NomeCliente.Text.Trim().Equals(string.Empty))
            return;
        var oCliente = new ClienteNG().Buscar(txt_NomeCliente.Text.Trim());
        if (oCliente == null)
        {
            bt_Excluir.Enabled = false;
            return;
        }
    }

    public void Confirmar()
    {
        Txt_NomeCliente_Validating_1(txt_NomeCliente, new CancelEventArgs());

        if (lvl_ListagemClientes.SelectedIndices.Count <= 0)
            return;
        var oCliente = new ClienteNG().Buscar(txt_NomeCliente.Text.Trim());
        var iSelectedIndex = Convert.ToInt32(lvl_ListagemClientes.SelectedIndices[0]);
        if (iSelectedIndex >= 0)
        {
            iRetorno = Convert.ToInt32(lvl_ListagemClientes.Items[iSelectedIndex].Text);
            txt_CodigoCliente.Text = Convert.ToInt32(oCliente.ClienteID).ToString();
            txt_cpfcnpj.Text = oCliente.CpfCnpj;
            mkd_Telefone.Text = oCliente.Telefone;
            txt_Rua.Text = oCliente.Rua;
            txt_Numero.Text = oCliente.Numero;
            cb_Bairro.Text = oCliente.Bairro;
            cb_Cidade.Text = oCliente.Cidade;
            tb_Observacoes.Text = oCliente.Observacao;
            mkd_LimiteTotal.Text = oCliente.Limite;
            label.Text = oCliente.Complemento;
            ////////////////////// Nao esta aparecendo os campos de dados
        }
    }

    private void Lvl_ListagemClientes_DoubleClick_1(object sender, EventArgs e)
    {
        Confirmar();
    }
  • Explaining in a clearer way, in my code when writing the client name in the textbox, search, and double click the listview on the searched client, it loads the textbox information. Only for that to happen, the name has to match the name saved in the database. My goal is not to load the data by the name typed in the textbox, but only by double click on the listview, for this, he would need to take the first data informed in the listview line that is the client code, and search in the database for that code. Thus, showing customer information by code

  • The idea would then be to display all the clients registered in Listview, and then load the client’s data in the Textbox when a double-click occurred?

No answers

Browser other questions tagged

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