WinForm 绑定单选按钮

WinForm binding radio button(WinForm 绑定单选按钮)

本文介绍了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.

绑定格式和解析事件

Binding 类有一个内置工具,用于以 格式 和 解析事件.

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

现在您可以简单地将 Male 单选按钮绑定到数据源上的此属性(只是不要在网格中显示此属性).

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 绑定单选按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:WinForm 绑定单选按钮

基础教程推荐