当父表单更改其当前项时,如何停止数据绑定子表单更改其当前项?

时间:2012-05-13 13:31:57

标签: c# winforms data-binding bindingsource

我有一个DataSet,其中包含一个“客户端”表和一个“预订”表。在两个“ClientsBookings”之间设置了DataRelation。

我已将Clients表绑定到父表单中的DataGridView。双击行时,将为所选客户端打开一个新的子表单,其中显示客户端详细信息及其相关的预订。

目前,每个新表单似乎都绑定到父表单中的当前项目。因此,当父表中的行发生更改时,每个打开的子表单中的当前项也会发生更改。我认为在子窗体中创建一个新的BindingSource并设置它的位置会使其当前项独立于父窗体的绑定源(它不起作用)。

目前,我使用“ClientsBookings”关系在子窗体中将两个BindingSource链接在一起。这样可确保仅在子表单中显示与当前客户端相关的预订。如果我改为传递DataRow或类似的东西,我无法弄清楚如何获得同样的效果。

无论如何,到目前为止,这是代码:

父表:

public partial class ClientsSearchForm : Form
{
    BindingSource bsClients = new BindingSource();

    private void ClientsSearchForm_Load(object sender, EventArgs e)
    {
        DataSet clientsDataSet = GetDataSet();
        bsClients.DataSource = myDataSet;
        bsClients.DataMember = "Clients";
        dataGridView1.DataSource = bsClients;

        this.dataGridView1.CellDoubleClick += new System.Windows.Forms.DataGridViewCellEventHandler(this.dataGridView1_CellDoubleClick);
    }

    private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
    {
        ClientForm form = new ClientForm(bsClients);
        form.Show();
    }

}

儿童表格

public partial class ClientForm : Form
{
private BindingSource bsClients = new BindingSource();
private BindingSource bsBookings = new BindingSource();

public ClientForm()
{
    InitializeComponent();
}

public ClientForm(BindingSource bsClients)
{
    private BindingSource bsClients = new BindingSource();
    private BindingSource bsBookings = new BindingSource();

    public ClientForm(BindingSource bs)
    {
        InitializeComponent();

        // set up client binding source
        // Note: intent is to have this form associated with only one client
        // and not change when the selection on the search forlm changes.
        this.bsClients.DataSource = bs.DataSource;
        this.bsClients.DataMember = bs.DataMember;
        this.bsClients.Position = bs.Position;

        // set up bookings binding source
        this.bsBookings.DataSource = this.bsClients;
        this.bsBookings.DataMember = "ClientsBookings";

        // set up control bindings
        textBox1.DataBindings.Add("Text", bsClients, "Given Name");
        dataGridView1.DataSource = bsBookings;
    }
}

0 个答案:

没有答案