Data query in a datagrid (registration program)

Asked

Viewed 53 times

1

The problem I’m having is that I don’t how to display in datagrid only the name of the users to be possible to query each registration in this database when selected, and then displaying all that data I have already put and more if the informed data was CPF or CNPJ.

I’m using Windows Forms Application with C#.

Below and I’m putting my code.

List<Pessoa> listcadastro = new List<Pessoa>();


    public class Pessoa
    {

        private string nome;
        private string endereço;
        private int ano_nascimento;
        private string telefone;
        private int idade;

        public string Nome
        {
            get
            {
                return nome;
            }

            private set
            {
                nome = value;
            }
        }

        public string Endereço
        {
            get
            {
                return endereço;
            }

            private set
            {
                endereço = value;
            }
        }

        public int Ano_nascimento
        {
            get
            {
                return ano_nascimento;
            }

            private set
            {
                ano_nascimento = value;
            }
        }

        public int Idade
        {
            get
            {
                return idade;
            }

            private set
            {
                idade = value;
            }
        }

        public string Telefone
        {
            get
            {
                return telefone;
            }

            private set
            {
                telefone = value;
            }
        }

        public Pessoa(string nome, string endereço, int ano_nascimento, string telefone)
        {
            this.nome = nome;

            this.endereço = endereço;

            this.ano_nascimento = ano_nascimento;

            this.telefone = telefone;

            this.idade = DateTime.Now.Year - ano_nascimento;

        }
    }

            //Classe Fisica herdada da classe Pessoa
    public class Fisica : Pessoa
    {
        private string nCPF;


        public Fisica(string nome, string endereço, int ano_nascimento, string telefone, string nCPF)
            : base(nome, endereço, ano_nascimento, telefone)
        {
            this.nCPF = nCPF;
        }

        public string CPF
        {
            get
            {
                return nCPF;
            }

            private set
            {
                nCPF = value;
            }
        }


    }

    //Classe Juridica herdada da classe Pessoa
    public class Juridica : Pessoa
    {
        private string nCNPJ;

        public Juridica(string nome, string endereço, int ano_nascimento, string telefone, string nCNPJ)
            : base(nome, endereço, ano_nascimento, telefone)
        {
            this.nCNPJ = nCNPJ;
        }

        public string CNPJ
        {
            get
            {
                return nCNPJ;
            }

            private set
            {
                nCNPJ = value;
            }
        }


    }


private void bt_salvar_Click(object sender, EventArgs e)
{

    listcadastro.Add(new Pessoa(txt_nome.Text, txt_endereco.Text, int.Parse(txt_ano.Text), txt_telefone.Text));

    dataGridView1.DataSource = null;
    dataGridView1.DataSource = listcadastro;


}





private void excluirToolStripMenuItem_Click(object sender, EventArgs e)
{

}
  • Are you making a mistake? Aside from the fact that you are not using C#’s GET and SET, as I’ve shown you in previous answers, I don’t see any major issues in your code.

  • Could you improve your doubt? They are confused understand what you want and where you are having problems.

  • I’ve updated my question and the code.

  • The @bigown response doesn’t suit you?

  • Is that I need to know how to query the data of each registration, but only display the name of the user in datagrid and after it is selected, then yes display the rest (and more Cpf/cnpj that I am not displaying yet).

1 answer

1

In fact this code can be greatly simplified only using properties. This way meets all needs described in the question.

And still change some other little cosmetic things.

What are you doing with the builder does not flame overload.

using System;

public class Program {
    public static void Main() {}
}

public class Pessoa {
    public string Nome { get; set; }
    public string Endereço { get; set; }
    public int Ano_nascimento { get; set; }
    public int Idade { get; set; }
    public string Telefone { get; set; }
    public Pessoa(string nome, string endereço, int ano_nascimento, string telefone) {
        Nome = nome;
        Endereço = endereço;
        Ano_nascimento = ano_nascimento;
        Telefone = telefone;
        Idade = DateTime.Now.Year - ano_nascimento;
    }
}

public class Fisica : Pessoa {  
    public string NmmCPF { get; private set; }
    public Fisica(string nome, string endereço, int ano_nascimento, string telefone, string nCPF)
        : base(nome, endereço, ano_nascimento, telefone) {
            this.NmmCPF = nCPF;
    }
}

public class Juridica : Pessoa {
    public string NumCnpj { get; private set; }
    public Juridica(string nome, string endereço, int ano_nascimento, string telefone, string nCNPJ)
        : base(nome, endereço, ano_nascimento, telefone) {
            NumCnpj = nCNPJ;
    }
}

Behold working in the ideone. And in the .NET Fiddle. Also put on the Github for future reference.

  • The only thing I would change here is to put the private set; to respect the encapsulation of the original code.

  • @Erickgallani is true, I ate ball in this. Thank you.

  • Is that I need to know how to query the data of each registration, but only display the name of the user in the datagrid and after it is selected, then yes display the rest (and more Cpf/cnpj I’m not displaying yet)

  • @Remember none of this is in the question, even this comment does not clarify anything about your problem.

  • I had written in the second paragraph of the question, now I excluded the first.

  • You said something over the top, but there’s none of that in your code.

  • Yeah, it’s because I don’t know how to do it. I was only able to display the data name, address, year and direct phone on the datagrid, not the way I explained that I’m trying.

  • Then the question is not suitable for the site, here we help people with difficulties, not to do for them. Look for a book, take a good course, then when you’re in doubt you can ask here.

  • But my question is: how to query these registration data from the name that was previously registered? I’m only able to show all the data directly, not only the name of the person and when I select it there yes open the registered data.

  • I made it on my own

Show 5 more comments

Browser other questions tagged

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