【问题标题】:Returning checkbox value from datagridview to form in c#从datagridview返回复选框值以在c#中形成
【发布时间】:2016-04-25 12:27:08
【问题描述】:

我试图在双击时从 dataGridView1 获取复选框值,除复选框外,每个字段都运行良好,所以我需要的是,当用户检查时,我希望能够将该检查从 dataGridView1 返回到表单请检查我的代码。

private void dataGridView1_MouseDoubleClick_1(object sender, MouseEventArgs e)
    {
        //All is working perfect here return from datagridview1
        to each text box
        DataGridViewRow dr = dataGridView1.SelectedRows[0];
        textBox1.Text = dr.Cells[0].Value.ToString();   //id
        textBox2.Text = dr.Cells[1].Value.ToString();   //name
        textBox3.Text = dr.Cells[2].Value.ToString();   //age
        comboBox1.Text = dr.Cells[3].Value.ToString();  //day
        comboBox2.Text = dr.Cells[4].Value.ToString();  //month
        comboBox3.Text = dr.Cells[5].Value.ToString();  //year
        textBox4.Text = dr.Cells[6].Value.ToString();   //reference
        textBox5.Text = dr.Cells[7].Value.ToString();   //weight
        textBox6.Text = dr.Cells[8].Value.ToString();   //height
        textBox7.Text = dr.Cells[9].Value.ToString();   //phonenumber
        textBox8.Text = dr.Cells[39].Value.ToString();  //address
        comboBox4.Text = dr.Cells[40].Value.ToString();  //gender
        textBox9.Text = dr.Cells[35].Value.ToString();  //insurance
        textBox10.Text = dr.Cells[36].Value.ToString(); //profession
        textBox11.Text = dr.Cells[37].Value.ToString(); //email
        dateTimePicker1.Text = dr.Cells[38].Value.ToString();   //datepicker
        textBox12.Text = dr.Cells[10].Value.ToString(); //question1
        textBox13.Text = dr.Cells[11].Value.ToString(); //treatment
        textBox14.Text = dr.Cells[12].Value.ToString(); //allergies
        textBox15.Text = dr.Cells[13].Value.ToString(); //surgicaloperations

        //my problem is here. datagrid view is returning true or false
        // i want it to return check or unchecked for checkbox
            checkBox1.Text = dr.Cells[14].Value.ToString();
            checkBox2.Text = dr.Cells[15].Value.ToString();
            checkBox3.Text = dr.Cells[16].Value.ToString();
            checkBox4.Text = dr.Cells[17].Value.ToString();
            checkBox5.Text = dr.Cells[18].Value.ToString();
            checkBox6.Text = dr.Cells[19].Value.ToString();
            checkBox7.Text = dr.Cells[20].Value.ToString();
            checkBox8.Text = dr.Cells[21].Value.ToString();
            checkBox9.Text = dr.Cells[22].Value.ToString();
            checkBox10.Text = dr.Cells[23].Value.ToString();
            checkBox11.Text = dr.Cells[24].Value.ToString();
            checkBox12.Text = dr.Cells[25].Value.ToString();
            checkBox13.Text = dr.Cells[26].Value.ToString();
            checkBox14.Text = dr.Cells[27].Value.ToString();
} 

【问题讨论】:

  • 如果为真,则表示CheckBox 被选中,如果为假,则未选中。那么你对这些结果有什么问题?
  • 你可能想要这个:checkBox1.Text = dr.Cells[14].Value? "Checked": "Unchecked";
  • @Draken 我想如果一个人勾选了复选框,当我双击 datagridview1 时,datagridvew1 应该返回这些刻度,它返回除了刻度之外的所有内容,只是说真或假
  • 您要返回图标吗?您为什么要这样做并将其放入文本字​​段中?
  • 我希望它返回一个复选框,如果选中和未选中的框未选中,我希望它是为了修改表格以供以后使用或更新...所以是的,我希望它返回您的问题的图标,thx 的帮助顺便说一句

标签: c# checkbox datagridview return


【解决方案1】:

你应该使用

checkBox1.Checked 

而不是

checkBox1.Text

【讨论】:

  • 它给出了一个错误:不能将类型'string'隐式转换为'bool'(在数据库定义中,复选框被定义为位)
  • dr.Cells[14].Value 没有转换 .ToString()
  • 在不转换为字符串的情况下也会出现同样的错误:/
  • 使用 Convert.ToBoolean(dr.Cells[14].Value)
  • 100% 工作!我的朋友,你太棒了,非常感谢!我返回所有选中的复选框,这就是我的想法
猜你喜欢
  • 2012-07-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多