1

我有一个包含“客户”表和“预订”表的数据集。两者之间建立了一个 DataRelation,“ClientsBookings”。

我已将 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;
    }
}
4

0 回答 0