简体   繁体   English

以编程方式在Master-Detail DataGridView的WinForms Master网格中选择一行并更新Detail DataGridView

[英]Programmatically select a row in WinForms Master grid of Master-Detail DataGridView and update Detail DataGridView

I have a Windows Forms application that displays information in a Master-Detail DataGridView, written based on the instructions at https://docs.microsoft.com/en-us/dotnet/framework/winforms/controls/create-a-master-detail-form-using-two-datagridviews . 我有一个Windows Forms应用程序,该应用程序在基于https://docs.microsoft.com/zh-cn/dotnet/framework/winforms/controls/create-a-master-上的说明编写的Master-Detail DataGridView中显示信息。 细节形式使用两个datagridviews

The data is displaying correctly, and selecting rows on the master DataGridView displays the expected data in the details DataGridView. 数据正确显示,并且选择主DataGridView上的行会在详细信息DataGridView中显示预期的数据。

What I am trying to do is pass in an integer when loading the page so that the DataGridViews will display with the right master row selected and the corresponding detail rows displayed. 我想做的是在加载页面时传递一个整数,这样DataGridViews就会显示正确的主行并显示相应的明细行。

So far I can pass in the integer to select the correct Master row, but one still needs to click the row to display the correct details rows. 到目前为止,我可以传递整数以选择正确的“主行”,但是仍然需要单击该行以显示正确的明细行。

Here is the constructor for the form: 这是表单的构造函数:

   public PalletList(User user, int orderId)
    {
        _user = user;
        InitializeComponent();
    }

In the Load() method, I populate the DGVs and Get Data for them. 在Load()方法中,我填充DGV并为其获取数据。 Then: 然后:

 foreach (DataGridViewRow row in ordersDataGridView.Rows)
  {
      if ((int)row.Cells["Id"].Value == orderId)
      {
        row.Selected = true;
        ordersDataGridView.FirstDisplayedScrollingRowIndex = row.Index;
      }
  }

Setting a DataGridViewRow's Selected property to true does nothing to change the BindingSource 's Position Property nor the BindingSource's Current Item. 将DataGridViewRow的Selected属性设置为true不会更改BindingSourcePosition属性或BindingSource的Current Item。 This makes sense as a DataGridView can have multiple selected rows ( SelectedRows Property ). 这很有意义,因为DataGridView可以具有多个选定行( SelectedRows属性 )。

The DataGridView does expose a CurrentCell property that would update the BindingSource's Position property. DataGridView确实公开了CurrentCell属性,该属性将更新BindingSource的Position属性。

So you should be either setting the `DataGridView.CurrentCell to reflect the desired row or BindingSource.Position Property to cause a change in the bindings. 因此,您应该设置DataGridView.CurrentCell以反映所需的行,或者设置BindingSource.Position属性以引起绑定的更改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM