Nouri Yacine
Nouri Yacine

Reputation: 63

send a value from datagridview to radiobutton in c#

I need to send a value from datagrideview to radibuttom in DoubleClick event

I'm using this code:

private void datagrideview1_DoubleClick(object sender, EventArgs e)
{
    if (this.datagrideview1.CurrentRow.Cells[5].Value.Equals("Female"))
        mrb_F.Checked = true;
    else
        mrb_F.Checked = false;

    if (this.datagrideview1.CurrentRow.Cells[5].Value.Equals("Male"))
        mrb_M.Checked = true;
    else
        mrb_M.Checked = false;
}

and it does not work for me!!

Upvotes: 0

Views: 1771

Answers (3)

Jameeel
Jameeel

Reputation: 11

I had a similar problem, i solved by grouping a pair of Radiobuttons. Like if you have 4 radiobuttons (2 for Gender & 2 for marital status) you must group one pair to avoid conflict because these button by default only one can be selected at ago. Improve it if i said wrongly.

Upvotes: -1

Muhammad Nasar
Muhammad Nasar

Reputation: 1

private void EmpDataGrid_MouseClick(object sender, MouseEventArgs e)
    {

        if (EmpDataGrid.SelectedRows[0].Cells[3].Value.Equals("Female"))
            FemaleRadioBtn.Checked = true;
        else
            FemaleRadioBtn.Checked = false;

        if (EmpDataGrid.SelectedRows[0].Cells[3].Value.Equals("Male"))
            MaleRadioBtn.Checked = true;
        else
            MaleRadioBtn.Checked = false;
     }

Upvotes: 0

blaze_125
blaze_125

Reputation: 2317

Here's a fully working sample that deals with the data types I think you are dealing with(based on your SQL query).

using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;

namespace CheckACheckBoxByClickingDataGridView_47102655
{
    public partial class Form1 : Form
    {
        BindingList<dgvEntry> dgvData = new BindingList<dgvEntry>();
        DataGridView dgv = new DataGridView();
        GroupBox gp = new GroupBox();
        CheckBox chkbx = new CheckBox();
        CheckBox chkbx2 = new CheckBox();
        CheckBox chkbx3 = new CheckBox();

        public Form1()
        {
            InitializeComponent();
            InitGrid();
            AddData();
            InitGroupBox();
        }

        private void InitGroupBox()
        {
            gp.Dock = DockStyle.Bottom;
            this.Controls.Add(gp);
            chkbx.Location = new Point(5, 5);
            chkbx2.Location = new Point(5, 25);
            chkbx3.Location = new Point(5, 45);
            gp.Controls.Add(chkbx);
            gp.Controls.Add(chkbx2);
            gp.Controls.Add(chkbx3);
        }

        private void AddData()
        {
            for (int i = 0; i < 10; i++)
            {
                dgvData.Add(new dgvEntry { id = i, age = i + 20, fullname = $"Name{i}", sex = i > 4? "Male" : "Female", date = DateTime.Now});
            }
        }

        private void InitGrid()
        {
            dgv.Dock = DockStyle.Top;
            dgv.AutoGenerateColumns = true;
            dgv.DataSource = dgvData;
            this.Controls.Add(dgv);
            dgv.DoubleClick += Dgv_DoubleClick;
        }

        private void Dgv_DoubleClick(object sender, EventArgs e)
        {
            if (dgv.CurrentRow.Cells[3].FormattedValue.Equals("Male"))
            {
                chkbx.Checked = true;
            }
            else
            {
                chkbx.Checked = false;
            }
        }
    }


    public class dgvEntry
    {
        public int id { get; set; }
        public string fullname { get; set; }
        public int age { get; set; }
        public string sex { get; set; }
        public DateTime date { get; set; }
    }
}

Upvotes: 2

Related Questions