主细节datagridviews:将焦点从详细记录移动到非父节点的主记录时RowValidating错误行为 [英] Master-detail datagridviews : RowValidating wrong behavior when moving focus from detail record to a master record which is not its parent

查看:62
本文介绍了主细节datagridviews:将焦点从详细记录移动到非父节点的主记录时RowValidating错误行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的WinForms应用中。我有master-detail DataGridViews。我已将RowValidating事件处理程序附加到我的详细信息网格中以进行一些验证和计算。



只要详细记录失去焦点,RowValidating就会触发。



问题是:



当我将注意力集中在细节记录上(用于编辑或其他)时,然后将焦点移至主控通过单击不是THAT详细记录的父记录的网格,这就是:



first:触发该详细记录的RowValidating事件,这是逻辑(因为它失去了焦点)。



然后:它的父记录的RowValidating事件被触发,这也是合乎逻辑的(因为我们将焦点移到新父节点) 。



然后:详细记录的RowValiding事件再次被触发!!! RowIndex与先前失去焦点的详细记录的索引相同,但现在详细网格中存在的实际细节是NEW父级(我们点击了)的详细信息!这不是逻辑上的。



i尝试了其他事件(CellLeave,RowLeave)并且它们的行为相同:

详细事件首先被触发丢失焦点的细节行,

然后为其父级触发主事件,

然后详细事件被触发AGAIN,但是有关新父节点和RowIndex的详细信息旧的详细记录...



为什么详细事件被解雇两次?为什么他们使用之前详细记录的RowIndex?



P.S.我下载了一个主 - 细节DGV演示,并尝试了相同的事件,并发生了类似的事情。

演示:



三个与主要细节相关的网格 [ ^ ]

in my WinForms app. i have master-detail DataGridViews. i have attached a RowValidating event handler to my detail grid to make some validation and calculations.

RowValidating fires whenever a detail record loses focus.

the problem is:

when i set focus on a detail record (for editing or whatever) , and then move focus to master grid by clicking on a record that is NOT the parent of THAT detail record, this is what happens:

first : RowValidating event for that detail record is fired, which is logical (because it lost focus).

then : RowValidating event for its parent record is fired, which is logical, too (because we moved focus to a new parent) .

and then : RowValiding event for detail record is fired AGAIN !!! RowIndex is the same index of the detail record which previously lost focus, BUT the actual details that are present in the detail grid now are details of the NEW parent (which we clicked on)! THIS IS NOT LOGICAL.

i have tried other events (CellLeave, RowLeave) and they act the same :
detail events are fired first for the detail row which lost focus,
then master events are fired for its parent,
then detail events are fired AGAIN, but for details of the new parent and RowIndex of the old detail record ...

WHY are detail events are fired twice? and why are they using RowIndex of the previous detail record ?

P.S. i downloaded a master-detail DGV demo, and tried the same events, and similar things happened.
DEMO:

Three Master-Details Related Grids on the Same Form[^]

推荐答案

private void detailGrid_RowValidating(object sender,DataGridViewCellCancelEventARgs e)
{

   string current_parent_row_id =  masterGrid.CurrentRow.Cells["id"].Value.ToString();
   string validated_parent_row_id = detailGrid.Rows[0].Cells["id"].Value.ToString();

   if (current_parent_row_id != validated_parent_row_id) return;

   // validation goes next ...

}


这篇关于主细节datagridviews:将焦点从详细记录移动到非父节点的主记录时RowValidating错误行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆