Save selected radio button option for future query

Asked

Viewed 362 times

2

I wonder if you can save in one DataTable (or something like, to be consulted later) information that a radioButton was clicked. I am making a simple program for a client to report three information in three textbox this is saved in a DataTable and is shown in a DataGrid. Also, I’d like to show you which radioButton (if the radiobutton of physical person is selected, then the txt_registro will receive a number of Cpf)was clicked on the registration that the user is viewing on DataGrid (the user can consult the registration information after only clicking on the line dataGridView.

I thought maybe this should be done within the class, in a method, but I’m not sure because I still have to make an option to edit the selected registration (so the user can edit what he had written in an old register through the text box and save in the same place as the old register.

public int con;

DataSet ds = new DataSet();
DataTable dt = new DataTable("Cadastro");


public Form1()
{
    InitializeComponent();

    dt.Columns.Add("Nome", Type.GetType("System.String"));
    dt.Columns.Add("Email", Type.GetType("System.String"));
    dt.Columns.Add("Registro", Type.GetType("System.String"));

    dataGridView1.DataSource = dt;
}

private void bt_salvar_Click_1(object sender, EventArgs e)
{

    DataRow dr = dt.NewRow();

    if (con == 1) 
    {
        Fisica dadosfisica = new Fisica(txt_nome.Text, txt_email, txt_registro.Text);
        dr["Registro"] = "CPF: " + dadosfisica.CPF;
        dr["Nome"] = dadosfisica.Nome;
        dr["Endereco"] = dadosfisica.Email;

    }

    if (con == 2)
    {
        Juridica dadosjuridica = new Juridica(txt_nome.Text, txt_email,txt_registro.Text);
        dr["Registro"] = "CNPJ: " + dadosjuridica.CNPJ;
        dr["Nome"] = dadosjuridica.Nome;
        dr["Endereco"] = dadosjuridica.Email;

    }


    dt.Rows.Add(dr);
    dataGridView1.DataSource = dt;


}>



                 <public class Dados
            {

            private string _nome;
            private string _email;

            //construtor para iniciar os dados privados - recebe parametros
            public Dados(string nome, string email)
            {
             this._nome = nome;

             this._email = email;
            }


            public string Nome
            {
             get
             {
                 return _nome;
             }
             set
             {
                 _nome = value;
             }
            }

            public string Email
            {
             get
             {
                 return _email;
             }
             set
             {
                 _email = value;
             }
            }
            }>

              <//Classe Juridica herdada da classe Dados
            public class Juridica : Dados
            {

                private string nCNPJ;

                public Juridica(string nome, string email, string nCNPJ)
                    : base(nome, email) //chama o construtor da classe base
                {
                    this.nCNPJ = nCNPJ;
                }

                public string CNPJ
                {
                    get{return nCNPJ;}

                    set{nCNPJ = value;}
                }

            }>


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

                public Fisica(string nome, string email, string nCPF)
                    : base(nome, email) //chama o construtor da classe base
                {
                    this.nCPF = nCPF;
                }

                public string CPF
                {
                    get{return nCPF;}

                    set{nCPF = value;}
                }

            }>



//Seleção do RadioButton
private void bt_pfisica_CheckedChanged_1(object sender, EventArgs e)
{
    if (bt_pfisica.Checked)
    {
        txt_registro.Enabled = true;
        txt_registro.Mask = "000,000,000-00";
        con = 1;
    }
}

private void bt_pjuridica_CheckedChanged_1(object sender, EventArgs e)
{
    if (bt_pjuridica.Checked)
    {
        txt_registro.Enabled = true;
        txt_registro.Mask = "00,000,000/0000-00";
        con = 2;
    }
}

1 answer

2

You can save a symbolic value in the Datatable, indicating whether it was clicked or not, example:

dr["Registro"] = "CPF: " + dadosfisica.CPF;
dr["Nome"] = dadosfisica.Nome;
dr["Endereco"] = dadosfisica.Email;
//Crie uma coluna para salvar o dado pretendido. No caso, usarei o valor '1' para 'clicado' e '0' para não clicado
dr["PessoaFisica"] = dadosfisica.Juridica ? "1" : "0";

And then, you can create a boolean attribute in your class to save that information:

private bool bJuridica;
public bool PessoaJuridica
{
      get{return bJuridica;}

      set{bJuridica = value;}
}

, So you can set the value in the attribute like this:

dados.Juridica = bt_pjuridica.Checked;

I hope I understand your doubt correctly

  • 1

    Actually the idea is not to show in column, but to show the radiobutton "checked" when I select the register line in the dataGridView.

  • 1

    The dr["Personal"] = data. Legal ? "1" "0" is giving problems to find the place of Legal.

  • 1

    Yes, you have to add the column in the DataTable and the attribute in the Class. To set the radiobutton to "checked", you do: bt_pjuridica.Checked = true;, if you find the necessary condition

  • 1

    Can you not add the column? Because I just want to see the radiobutton selected when I click on a datagrid line (which has a record) and the value of the record will already be in a column.

  • 1

    If you don’t need to store the information somewhere, you don’t need to add the column to the Datatable.

  • I just need to save the information of which radiobutton was clicked on each register.

  • So, the way you seem to be developing, I recommend saving which Radiobutton was clicked on Datatable along with the other data.

  • But then I’ll need to show in the column, no?

  • You can put the column of Datagridview as .Visible = false;, so it will not be visible to the user

  • Ah, I didn’t know that function! Thank you, I’ll use!!

  • For nothing :). Remember that the data will continue there, only invisible to the user

Show 7 more comments

Browser other questions tagged

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