Bring database to RadioButton

0

Greetings, I have a datagridview with an edit button. When you press to edit, the button brings all the data from the database to the form called Update.

With the text fields it is simple. I was able to do the conversion in this way:

    private void dgv_buscar_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
    {
        if (e.RowIndex !=1)
        {
            if (dgv_buscar.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString().Equals("editar"))
            {
                var registro = new Clases.Registro();
                var cedula = dgv_buscar.Rows[e.RowIndex].Cells[3].Value.ToString();
                var tabla = registro.BUSCARCEDULA(cedula);
                var f = new Actualizar();
                f.Show();
                if (tabla.Rows.Count==1)
                {
                    Actualizar.Myform.txtNombre.Text = tabla.Rows[0]["NOMBRE"].ToString();
                    Actualizar.Myform.txtApellido.Text = tabla.Rows[0]["APELLIDO"].ToString();
                    Actualizar.Myform.txtCedula.Text = tabla.Rows[0]["CEDULA"].ToString();
                    Actualizar.Myform.txtDireccion.Text = tabla.Rows[0]["DIRECCION"].ToString();
                    Actualizar.Myform.txtTelefono.Text = tabla.Rows[0]["TELEFONO"].ToString();
                    Actualizar.Myform.txtCelular.Text = tabla.Rows[0]["CELULAR"].ToString();
                    Actualizar.Myform.txtconversion.Text= tabla.Rows[0]["FCONVERSION"].ToString();
                    Actualizar.Myform.txtbautismo.Text = tabla.Rows[0]["FBAUTISMO"].ToString();
                    Actualizar.Myform.txtmiembro.Text = tabla.Rows[0]["FMIEMBRO"].ToString();
                    Actualizar.Myform.comboBox1.Text = tabla.Rows[0]["CONGREGACION"].ToString();
                    Actualizar.Myform.txtnacimiento.Text = tabla.Rows[0]["F_NACIMIENTO"].ToString();

Now, I need to bring the results that I inserted into the database through a radio, back to the radio and I have no idea how to do it.

To insert in a radio, I used two ways:

  • Declare a string with the name of RadioButton, and in case this radius is selected, insert the event result checked changed within it.
  • Example:

    String gender: genre="M";

  • The other method that I used was to declare in the database the field as bool, and to enter to the base that if the radius is selected, enter true or false.
  • This is the way I entered the database:

          private void btm_actualizar_Click(object sender, EventArgs e)
        {
            DateTime nacimiento = DateTime.Parse(txtnacimiento.Text);
            DateTime conversion = DateTime.Parse(txtconversion.Text);
            DateTime bautismo = DateTime.Parse(txtbautismo.Text);
            DateTime miembro = DateTime.Parse(txtmiembro.Text);
            var registrar = new Clases.Registro(txtCedula.Text, txtNombre.Text, txtApellido.Text, txtTelefono.Text, txtDireccion.Text, genero, ecivil, hijos, txtCelular.Text, nacimiento);
            registrar.registrar();
            var info = new Clases.InfoEcle(txtCedula.Text, conversion, bautismo, espiritusanto, bautizoiglesia, miembro, cajatexto);
            info.registrar();
            registrar.ListarDataGrid(Vistas.Buscar.FrmBuscar.dgv_buscar);
        }
        private void rdhombre_CheckedChanged(object sender, EventArgs e)
        {
    
            genero = "H";
        }
    
        private void rdmujer_CheckedChanged(object sender, EventArgs e)
        {
            genero = "M";
        }
    
        
    asked by Maicol Lenin 14.08.2017 в 22:35
    source

    1 answer

    0

    For what I was looking at you have two radioButtons one is the one that infers the gender of the Man to what you put "H" and the one of the gender of the Woman that is the M, you can do two things at the time of saving that information in the database 1 save it as a varchar (1), or save it as a bit or boolean. If you save it as a boolean, you only have to put it at the time of saving instead of passing the genre to put (gender == "H") causing it to take the Boolean value true if it is male and false if it is female and at the time of showing the Data extracted from the database you just have to do the following.

       Actualizar.Myform.rdhombre.Checked = tabla.Rows[0]["GENERO"].ToBoolean();
       Actualizar.Myform.rdmujer.Checked = !Actualizar.Myform.rdhombre.Checked;
       /*De esta forma si la persona es un hombre activas el radio button
         perteneciente a ese genero y desactivas el del genero contrario y si 
         es al reves tambien funciona, ya que asigna el valor contrario.*/
    

    I do not know if the ToBoolean function exists, maybe it's ToBool but well, I'll leave you with my contribution, if you still want to continue with the string data type, you do it this way

       Actualizar.Myform.rdhombre.Checked = tabla.Rows[0]["GENERO"].ToString().Equals("H");
       //Esta línea se mantiene en ambos casos
       Actualizar.Myform.rdmujer.Checked = !Actualizar.Myform.rdhombre.Checked;
    
        
    answered by 15.08.2017 в 23:21