0
votes

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.

The data is displaying correctly, and selecting rows on the master DataGridView displays the expected data in the details 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.

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. Then:

 foreach (DataGridViewRow row in ordersDataGridView.Rows)
  {
      if ((int)row.Cells["Id"].Value == orderId)
      {
        row.Selected = true;
        ordersDataGridView.FirstDisplayedScrollingRowIndex = row.Index;
      }
  }
1
Show how you are using the integer to select the Master Row.TnTinMn

1 Answers

0
votes

Setting a DataGridViewRow's Selected property to true does nothing to change the BindingSource's Position Property nor the BindingSource's Current Item. This makes sense as a DataGridView can have multiple selected rows (SelectedRows Property).

The DataGridView does expose a CurrentCell property that would update the BindingSource's Position property.

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.