将数据添加到数据源的UI具有的BindingSource [英] Adding data to DataSource from UI with BindingSource

查看:168
本文介绍了将数据添加到数据源的UI具有的BindingSource的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个窗体(Form),其中包含一个网格,通​​过的BindingSource绑定到数据源。

I have a form (Form1) which contains a grid, bound to a DataSource via a BindingSource.

然后我有一个按钮,一旦点击打开一个窗体(Form),应该让用户输入新数据。

I then have a button, which once clicked opens another form (Form2) that should let a user enter new data.

我通过的BindingSource从Form 1以窗体2,而我们的目标是,一旦用户拯救他的Form2的输入,它会被自动添加到Form1。

I pass the BindingSource from Form1 to Form2, and the goal is that once the user "saves" his input in Form2, it'll be automatically added to Form1.

有没有办法做到这一点,W / O直接访问UI控件?

Is there a way to do this, w/o directly accessing the UI controls?

即 -

public partial class Form2 : Form
{
    BindingSource bs = new BindingSource();
    public Form2(BindingSource bindingSourceFromForm1)
            {
                InitializeComponent();
                this.bs = bindingSourceFromForm1;
            }    
    private void button1_Click(object sender, EventArgs e)
            {
                DataRow dr = (this.bs.DataSource as DataTable).NewRow();
                dr["Col1"] = this.textBox1.Text;
                dr["Col2"] = this.textBox2.Text;
                this.bs.Add(dr);
            }
}

有没有办法绑定Form 2上的控制(在TextBox1中/ 2上面的例子中)到BindingSource,然后让它自动textBox1的&放添加值; 2?

Is there a way to bind the controls on Form2 (in the example above textBox1/2) to the BindingSource, and then have it automatically add the values in textBox1 & 2?

喜欢的东西叫 this.bs.Add(),其中添加()知道在哪里把它的值没有我明确地告诉它要到文本框,因为它绑定到上述管制?

Something like calling this.bs.Add(), where Add() knows where to take it's values without me explicitly telling it to go to the textboxes, since it's bound to the aforementioned controls?

谢谢!

推荐答案

如果您添加的BindingSource 到窗体设计器,你通常会做,将数据源同一来源,所以你可以绑定的文本框。

If you add a BindingSource to the form designer as you would normally do, set the DataSource to the same source so you can bind the textboxes.

在专门的构造函数如下code增加了一个新的记录到数据源 Form1的,分配数据源的数据源的这种形式BindingeSource 实例,并设置位置。新窗体将启用在新对象输入值的用户。

In the specialized constructor the following code adds a new record to the DataSource of form1, assigns the DataSource to the DataSource of the BindingeSource instance of this form and sets the position. Your new Form will enable the user the enter the values in that new object.

public Form2(BindingSource bindingSourceFromForm1)
    : this()
{
    bindingSourceFromForm1.AddNew();
    this.bindingSource1.DataSource = bindingSourceFromForm1.DataSource;
    this.bindingSource1.Position = bindingSourceFromForm1.Position;
}

如果您的用户可以通过调用<取消你要补偿该操作href="https://msdn.microsoft.com/en-us/library/system.windows.forms.bindingsource.removecurrent(v=vs.110).aspx"相对=nofollow> RemoveCurrent 上的 bindingSourceFromForm1 ,但我将它作为excersice,因为它是不清楚,如果你想/需要的。

If your user can cancel the operation you have to compensate for that by calling RemoveCurrent on the bindingSourceFromForm1 but I leave that as excersice as it is not clear if you want/need that.

这篇关于将数据添加到数据源的UI具有的BindingSource的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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