从datagridview向c#中的radiobutton发送一个值

时间:2017-11-03 18:51:37

标签: c# datagridview

我需要在DoubleClick事件

中将datagrideview中的值发送到radibuttom

我正在使用此代码:

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;
}

它对我不起作用!!

3 个答案:

答案 0 :(得分:1)

这是一个完整的工作示例,它处理我认为您正在处理的数据类型(基于您的SQL查询)。

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; }
    }
}

答案 1 :(得分:0)

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;
     }

答案 2 :(得分:-1)

我有一个类似的问题,我通过将一对单选按钮分组来解决了。就像您有4个单选按钮( 2个表示性别,2个表示婚姻状态)一样,您必须将一对按钮组合在一起以避免冲突,因为默认情况下,这些按钮以前只能选择一个。如果我说错了,请改进它。