Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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[^]
Posted
Updated 29-Sep-14 22:17pm
v5
Comments
George Jonsson 30-Sep-14 4:48am    
Are you using BindingSource as data source for your DataGridViews or do you connect them directly to DataSet and/or DataTable?
Not sure if it relevant in this case, but I have had problems when not using BindingSource in other cases.
nina4ever 30-Sep-14 5:35am    
it is the same code i had previously posted here (the code in the solution not in the question):

http://www.codeproject.com/Questions/817587/Master-detail-datagridview-how-to-insert-master-re?arn=0

it has BindingSource
nina4ever 1-Oct-14 3:00am    
thank u for ur concern. i will post the solution ASAP :)
Sinisa Hajnal 30-Sep-14 6:30am    
I can only give general advice on this and hope it works :)
1. try step-into debugger option
2. check sender and event parameters to find if you can distinguish between the two validations(maybe as simple as using master id from master and details to exit the event if they differ)
nina4ever 30-Sep-14 6:34am    
thank u.

i thought of that, but isn't it a workaround? i mean, is the behavior i am getting logical and correct?

1 solution

C#
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 ...

}
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900