[英]Programmatically select a row in WinForms Master grid of Master-Detail DataGridView and update Detail DataGridView
我有一个Windows Forms应用程序,该应用程序在基于https://docs.microsoft.com/zh-cn/dotnet/framework/winforms/controls/create-a-master-上的说明编写的Master-Detail DataGridView中显示信息。 细节形式使用两个datagridviews 。
数据正确显示,并且选择主DataGridView上的行会在详细信息DataGridView中显示预期的数据。
我想做的是在加载页面时传递一个整数,这样DataGridViews就会显示正确的主行并显示相应的明细行。
到目前为止,我可以传递整数以选择正确的“主行”,但是仍然需要单击该行以显示正确的明细行。
这是表单的构造函数:
public PalletList(User user, int orderId)
{
_user = user;
InitializeComponent();
}
在Load()方法中,我填充DGV并为其获取数据。 然后:
foreach (DataGridViewRow row in ordersDataGridView.Rows)
{
if ((int)row.Cells["Id"].Value == orderId)
{
row.Selected = true;
ordersDataGridView.FirstDisplayedScrollingRowIndex = row.Index;
}
}
将DataGridViewRow的Selected属性设置为true
不会更改BindingSource的Position属性或BindingSource的Current Item。 这很有意义,因为DataGridView可以具有多个选定行( SelectedRows属性 )。
DataGridView确实公开了CurrentCell属性,该属性将更新BindingSource的Position属性。
因此,您应该设置DataGridView.CurrentCell以反映所需的行,或者设置BindingSource.Position属性以引起绑定的更改。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.