【发布时间】:2015-08-07 20:59:53
【问题描述】:
我有一个包含网格的表单 (Form1),通过 BindingSource 绑定到 DataSource。
然后我有一个按钮,点击后会打开另一个表单 (Form2),让用户输入新数据。
我将 BindingSource 从 Form1 传递到 Form2,目标是一旦用户将他的输入“保存”在 Form2 中,它就会自动添加到 Form1。
有没有办法做到这一点,无需直接访问 UI 控件?
IE -
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);
}
}
有没有办法将 Form2 上的控件(在上面的示例中为 textBox1/2)绑定到 BindingSource,然后让它自动添加 textBox1 和 2 中的值?
类似于调用this.bs.Add(),其中Add() 知道在哪里获取它的值,而无需我明确告诉它转到文本框,因为它已绑定到上述控件?
谢谢!
【问题讨论】:
标签: c# .net winforms ado.net bindingsource