Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/heroku/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# WinForm绑定单选按钮_C#_Winforms_Data Binding_Datagridview_Radio Button - Fatal编程技术网

C# WinForm绑定单选按钮

C# WinForm绑定单选按钮,c#,winforms,data-binding,datagridview,radio-button,C#,Winforms,Data Binding,Datagridview,Radio Button,我使用VS2010,然后将成员datagridview拖放到design视图。 之后,我将名称成员文本字段拖放到设计视图,然后尝试编辑和保存。它工作正常 然后我拖放sex单选按钮来设计视图。但是绑定它不起作用 在这种情况下,我怎样才能进行绑定 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using Syst

我使用VS2010,然后将成员datagridview拖放到design视图。 之后,我将名称成员文本字段拖放到设计视图,然后尝试编辑和保存。它工作正常

然后我拖放sex单选按钮来设计视图。但是绑定它不起作用

在这种情况下,我怎样才能进行绑定

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Test7
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void memberBindingNavigatorSaveItem_Click(object sender, EventArgs e)
        {
            this.Validate();
            this.memberBindingSource.EndEdit();
            this.tableAdapterManager.UpdateAll(this.dbDataSet);

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'dbDataSet.Member' table. You can move, or remove it, as needed.
            this.memberTableAdapter.Fill(this.dbDataSet.Member);
            // TODO: This line of code loads data into the 'dbDataSet.Member' table. You can move, or remove it, as needed.
            this.memberTableAdapter.Fill(this.dbDataSet.Member);

        }


        private void memberBindingNavigatorSaveItem_Click_1(object sender, EventArgs e)
        {
            this.Validate();
            this.memberBindingSource.EndEdit();
            this.tableAdapterManager.UpdateAll(this.dbDataSet);

        }
    }
}

这里有两种可能的解决方案


绑定格式和解析事件

绑定
类有一个内置的工具,用于以和事件的形式动态转换绑定数据

下面是您将如何使用这些事件与“男性”单选按钮。在代码中而不是在设计器中创建绑定:

// create binding between "Sex" property and RadioButton.Checked property
var maleBinding = new Binding("Checked", bindingSource1, "Sex");
// when Formatting (reading from datasource), return true for M, else false
maleBinding.Format += (s, args) => args.Value = ((string)args.Value) == "M";
// when Parsing (writing to datasource), return "M" for true, else "F"
maleBinding.Parse += (s, args) => args.Value = (bool)args.Value ? "M" : "F";
// add the binding
maleRb.DataBindings.Add(maleBinding);

// you don't need to bind the Female radiobutton, just make it do the opposite
// of Male by handling the CheckedChanged event on Male:
maleRb.CheckedChanged += (s, args) => femaleRb.Checked = !maleRb.Checked;

计算属性

另一种方法是向数据源添加计算属性:

public bool IsMale
{ 
    get { return Sex == "M"; }
    set 
    {  
        if (value)
            Sex = "M";
        else
            Sex = "F";
    }
}
现在,您可以简单地将Male单选按钮绑定到数据源上的这个属性(只是不在网格中显示这个属性)

同样,你可以像这样把女性和男性联系起来:

maleRb.CheckedChanged += (s, args) => femaleRb.Checked = !maleRb.Checked;

虽然我意识到这一点已经得到了回答,但我想我会提供一个选项来提供设计时绑定能力

创建新的自定义单选按钮对象。这是通过以下代码完成的

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace MSLabExample
{
    class RadioButtonBind : RadioButton
    {
        private string _selectValue = string.Empty;
        public string SelectValue
        {
            get { return _selectValue; }
            set
            {
                if (value == Text) Checked = true;
                else Checked = false;
                _selectValue = value;
            }
        }

        public RadioButtonBind()
        {
            this.CheckedChanged += new EventHandler(RadioButtonBind_CheckedChanged);
            this.TextChanged += new EventHandler(RadioButtonBind_TextChanged);

        }
        void RadioButtonBind_TextChanged(object sender, EventArgs e)
        {
            if (Checked) _selectValue = Text;
        }

        void RadioButtonBind_CheckedChanged(object sender, EventArgs e)
        {
            if (Checked) _selectValue = Text;
        }
    }
}
上述控件的基本概念是使用可以绑定的字符串值,并将检查绑定的字符串值是否等于单选按钮文本

通过使用单选按钮文本,可以轻松理解正确的选中值,还可以通过简单地更改单选按钮文本来更新SelectValue。修改单选按钮时不需要额外代码

现在,只需将同一组中所有单选按钮的SelectedValue属性绑定到某个字符串属性

限制:

  • 同一组中的两个单选按钮不能具有相同的文本(但这真的是一个限制吗?)
  • 在设计时,单选按钮的检查值可能不准确(即,同一组中的两个单选按钮可能被检查)。但是,这不应该发生在运行时

  • 注意创建自定义控件时,必须在工具箱中提供自定义对象之前生成项目。

    制作表单时,可以从“数据源”面板拖动项。在“数据源”面板中,您可以创建一个类或表,它的所有子项都可以拖到表单中,并将自动生成一个文本框,或者在这种情况下生成一个带有数据绑定的单选按钮。在数据库或类中,您必须为每个选项创建一个bit/bool

    将单选按钮分组并添加一个不带数据绑定的单选按钮,以保持所有位/布尔为0

    将所有单选按钮的
    CausesValidation
    设置为False

    保存更改时,通过所有单选按钮循环,如:

    ((YourClass)myBindingSource.DataSource).property1 = radioButton1.Checked;
    ((YourClass)myBindingSource.DataSource).property2 = radioButton2.Checked;
    

    我认为这是的副本,因此,这里是我答案的副本。

    memberBindingNavigatorSaveItem\u Click和memberBindingNavigatorSaveItem\u Click有什么区别?我错过什么了吗+1用于花费时间发布代码和屏幕截图。这两个事件之间没有区别,您已经创建了一个新的Click事件,而该事件已经存在。您是否将单选按钮分组到GroupBox中,然后使用CheckedChanged事件?我想,这应该给你们足够的灵活性来完成这项工作。我尝试解决方案1,如果我设置所有记录都是女性,它将不会检查任何单选按钮。[]另一种解决方案是确保选中男性单选按钮的初始状态=true。这样,当绑定发生时,它将被更改为False,这将触发女性进行更新。在解决方案#2:是否有这样一个关键字
    属性
    ?@mmdemirbas:没有,谢谢您指出这一点。有时我的手指直接与我的大脑相连,我的大脑用错误的语言思考。也许这就是Delphi语法;许多年前我是一名Delphi程序员。