WinForm的结合单选按钮 [英] WinForm binding radio button

查看:663
本文介绍了WinForm的结合单选按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用VS2010,然后拖放会员datagridview的设计视图。
之后,我拖放名成员文本框设计视图,然后尝试编辑和保存。这是正常工作。

I use VS2010 and then drag and drop Member datagridview to design view. After that I drag and drop name member textfield to design view and then try to edit and save. It's work properly.

然后我拖放性别单选按钮设计视图。但结合简化版,它的工作。

And then I drag and drop sex radio button to design view. But binding it does't work.

我怎么能在这种情况下绑定?

How can I binding in this situation?

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

        }
    }
}



推荐答案

下面是两种可能的解决方案。

Here are two possible solutions.

绑定格式化和分析事件

绑定类有关于即时绑定数据的转换中的形式内置工具的< A HREF =http://msdn.microsoft.com/en-us/library/system.windows.forms.binding.format.aspx相对=nofollow>格式和的解析事件

The Binding class has a built-in facility for on-the-fly transformations of bound data in the form of the Format and Parse events.

下面是你将如何使用这些事件只有男性单选按钮。在代码中创建绑定,而不是在设计师:

Here's how you would use those events with just the "Male" radiobutton. Create the binding in code, not in the designer:

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






计算属性

另一种方法是将计算的属性添加到您的数据源:

Another approach is to add a computed property to your datasource:

public bool IsMale
{ 
    get { return Sex == "M"; }
    set 
    {  
        if (value)
            Sex = "M";
        else
            Sex = "F";
    }
}

现在你可以简单地将男单选按钮绑定到这个属性。在你的数据源(只是不显示在网格中该属性)

Now you can simply bind the Male radiobutton to this property on your datasource (just don't show this property in the grid).

和你可以再次女钩到男像这样:

And again you can hook up Female to Male like so:

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

这篇关于WinForm的结合单选按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆