Recent

Author Topic: [SOLVED]DBGrid EditingDone event  (Read 1336 times)

kito

  • Jr. Member
  • **
  • Posts: 79
[SOLVED]DBGrid EditingDone event
« on: February 06, 2021, 09:39:20 pm »
Hello , Hey Dude's ..

I am working on inventory management project , in the selling form  I made a DBGRID  so the User can easily manipulate the operations .


When the user changes the Quantity (Add / Subtract ) I thought it will be easier to make A function Gets the initial Value of the current quantity and make a comparison  to the new insert Value in order to  add or subtract the difference from the stock .

When I applied the idea putting the function in ( on editing done  event ) of the dbgird I faced  a huge problem once the user clicked another cell  the function keeps executing ... unless the user before exiting the editted cell   pressed a key  that issues cause a big mess in the stock quantities .

How can I  finish the editing before the user clicks another cell ?
Or How can I force the user to press a key instead of using the mouse ?
Any hint for a better idea about changing the Quantities is so welcome




Lazarus 2.0.10
Win 10  X64 bit
This is a sample of the code  :
 


Code: Pascal  [Select][+][-]
  1.  
  2.   QU :Integer ; // public
  3.  
  4. procedure TForm5.IBDynamicGrid1EditingDone(Sender: TObject);
  5. var
  6.     w : Integer ;
  7. begin
  8.  
  9.   if DataModule1.OpsaleBillGlobal.IsEmpty then Exit;
  10.    if DataModule1.OpsaleBillGlobalOPERQUANT.AsInteger = 0 then DataModule1.OpsaleBillGlobal.Delete;
  11.                      if  form5.IBDynamicGrid1.DataSource.DataSet.RecordCount = 0 then  exit ;
  12.          if  QU = DataModule1.OpsaleBillGlobalOPERQUANT.AsInteger then  exit ;
  13.         w:=  QU ;
  14.      if  DataModule1.OpsaleBillGlobalOPERQUANT.asinteger = 0 then
  15.        begin
  16.         DataModule1.productsINISIAL.Edit;
  17.             DataModule1.productsINISIALQUANTITY_PRODUCT.AsInteger:=  DataModule1.productsINISIALQUANTITY_PRODUCT.AsInteger +  DataModule1.OpsaleBillGlobalID_SALE.AsInteger ;
  18.        DataModule1.OpsaleBillGlobal.Delete ;
  19.           DataModule1.productsINISIAL.ApplyUpdates;
  20.                DataModule1.OpsaleBillGlobal.ApplyUpdates;
  21.                 DataModule1.OPERSALE.Refresh;
  22.                  DataModule1.OpsaleBillGlobal.Refresh;
  23.     DataModule1.products.Refresh;
  24.     Exit;
  25.          end;
  26.        if  DataModule1.OpsaleBillGlobalOPERQUANT.asinteger > (DataModule1.productsINISIALQUANTITY_PRODUCT.AsInteger+ QU) then
  27.           begin
  28.            DataModule1.OpsaleBillGlobal.Edit;
  29.            DataModule1.OpsaleBillGlobalOPERQUANT.asinteger :=  W;
  30.            qu :=  W;
  31.            DataModule1.productsINISIAL.ApplyUpdates;
  32.             DataModule1.OpsaleBillGlobal.ApplyUpdates;
  33.          Exit;
  34.           end;
  35.          if  DataModule1.OpsaleBillGlobalOPERQUANT.asinteger <= (DataModule1.productsINISIALQUANTITY_PRODUCT.AsInteger+ QU )then
  36.          begin
  37.        DataModule1.productsINISIAL.Edit;
  38.     DataModule1.productsINISIALQUANTITY_PRODUCT.AsInteger:=  DataModule1.productsINISIALQUANTITY_PRODUCT.AsInteger + ( QU -DataModule1.OpsaleBillGlobalOPERQUANT.AsInteger);
  39.     DataModule1.productsINISIAL.ApplyUpdates;
  40.         DataModule1.OpsaleBillGlobal.ApplyUpdates;
  41.          DataModule1.products.Close;
  42.          DataModule1.products.Open;
  43.        QU := DataModule1.OpsaleBillGlobalOPERQUANT.AsInteger ;
  44.          end;
  45.           DataModule1.OpsaleBillGlobal.ApplyUpdates;
  46.  
  47.    DataModule1.fixoperation();
  48.     DataModule1.products.Refresh;
  49. end;                                
  50.  
  51.  
  52.  


« Last Edit: February 07, 2021, 06:35:17 pm by kito »
We can barely feel your presence, because of the depth of your silence

egsuh

  • Hero Member
  • *****
  • Posts: 1597
Re: DBgird EditingDone event
« Reply #1 on: February 07, 2021, 06:57:14 am »
Your explanation is not clear to me. In most cases EditingDone is fired when the focus moves away -- by clicking another cell or clicking other control, etc. While the cursor is still in the same cell the EditingDone should not be called. Not clear how you could call EditingDone by pressing a key.

kito

  • Jr. Member
  • **
  • Posts: 79
Re: DBgird EditingDone event
« Reply #2 on: February 07, 2021, 11:05:56 am »
thank you egsuh  for  your replay I  am sorry I couldn't explain clearly
let me try again to explain step by step:

when I edit the quantity in the sell_bill here what happens :

1-saving the current quantity  using public integer QU before any modification :

Code: Pascal  [Select][+][-]
  1. procedure TDataModule1.OpsaleBillGlobalBeforeEdit(DataSet: TDataSet);
  2. begin
  3. QU :=  DataModule1.OpsaleBillGlobalOPERQUANT.AsInteger ;
  4. end;
  5.  

2- make a comparison between  QU and the New insert value in order to fix the stock quantity for the current product :

Code: Pascal  [Select][+][-]
  1. procedure TForm5.IBDynamicGrid1EditingDone(Sender: TObject);
  2. var
  3.     w : Integer ;
  4. begin
  5.  
  6.   if DataModule1.OpsaleBillGlobal.IsEmpty then Exit;
  7.    if DataModule1.OpsaleBillGlobalOPERQUANT.AsInteger = 0 then DataModule1.OpsaleBillGlobal.Delete;
  8.                      if  form5.IBDynamicGrid1.DataSource.DataSet.RecordCount = 0 then  exit ;
  9.          if  QU = DataModule1.OpsaleBillGlobalOPERQUANT.AsInteger then  exit ;
  10.         w:=  QU ;
  11.      if  DataModule1.OpsaleBillGlobalOPERQUANT.asinteger = 0 then
  12.        begin
  13.         DataModule1.productsINISIAL.Edit;
  14.             DataModule1.productsINISIALQUANTITY_PRODUCT.AsInteger:=  DataModule1.productsINISIALQUANTITY_PRODUCT.AsInteger +  DataModule1.OpsaleBillGlobalID_SALE.AsInteger ;
  15.        DataModule1.OpsaleBillGlobal.Delete ;
  16.           DataModule1.productsINISIAL.ApplyUpdates;
  17.                DataModule1.OpsaleBillGlobal.ApplyUpdates;
  18.                 DataModule1.OPERSALE.Refresh;
  19.                  DataModule1.OpsaleBillGlobal.Refresh;
  20.     DataModule1.products.Refresh;
  21.     Exit;
  22.          end;
  23.        if  DataModule1.OpsaleBillGlobalOPERQUANT.asinteger > (DataModule1.productsINISIALQUANTITY_PRODUCT.AsInteger+ QU) then
  24.           begin
  25.            DataModule1.OpsaleBillGlobal.Edit;
  26.            DataModule1.OpsaleBillGlobalOPERQUANT.asinteger :=  W;
  27.            qu :=  W;
  28.            DataModule1.productsINISIAL.ApplyUpdates;
  29.             DataModule1.OpsaleBillGlobal.ApplyUpdates;
  30.          Exit;
  31.           end;
  32.          if  DataModule1.OpsaleBillGlobalOPERQUANT.asinteger <= (DataModule1.productsINISIALQUANTITY_PRODUCT.AsInteger+ QU )then
  33.          begin
  34.        DataModule1.productsINISIAL.Edit;
  35.     DataModule1.productsINISIALQUANTITY_PRODUCT.AsInteger:=  DataModule1.productsINISIALQUANTITY_PRODUCT.AsInteger + ( QU -DataModule1.OpsaleBillGlobalOPERQUANT.AsInteger);
  36.     DataModule1.productsINISIAL.ApplyUpdates;
  37.         DataModule1.OpsaleBillGlobal.ApplyUpdates;
  38.          DataModule1.products.Close;
  39.          DataModule1.products.Open;
  40.        QU := DataModule1.OpsaleBillGlobalOPERQUANT.AsInteger ;
  41.          end;
  42.           DataModule1.OpsaleBillGlobal.ApplyUpdates;
  43.  
  44.    DataModule1.fixoperation();
  45.     DataModule1.products.Refresh;
  46. end;    
  47.  
  48.  
  49.  

it works correctlly when I press Enter atfter Editting .

but when I  just click another cell (to change the focus )it  effects on the Quantity of the  new selected Product Using the Old  QU



« Last Edit: February 07, 2021, 04:40:03 pm by kito »
We can barely feel your presence, because of the depth of your silence

egsuh

  • Hero Member
  • *****
  • Posts: 1597
Re: DBGrid EditingDone event
« Reply #3 on: February 07, 2021, 04:00:17 pm »
Lazarus developers will hate me if I write this^^, but I write...

Please check the order of following events occur -- BeforeEdit, EditingDone, BeforePost, AfterPost, BeforeScroll, and AfterScroll. You can check these by inserting showmessage or adding text to a TMemo etc.  The text had better contain some data that can identify which record it is, for example, ShowMessage('BeforeEdit called - ' + DataModule1.OpsaleBillGlobalOPERQUANT.asString); . 

Intuitively they should occur in the order I wrote above.

Your problem can be explained if EditingDone is called after AfterScroll.

If this is the case, defining them in BeforeScroll or BeforePost instead of EditingDone may solve the problem. 
If not, we have to think about other possibilities, but still you'd better check the record that is accessed in the EditingDone procedure.

I think I had similar problem in the past, but not sure about which handler --- Anyway you should check the order that eventhandlers are called when you use eventhandlers. They often operate in not intuitive ways --- this is rather serious problem I think, but this seems to be at lower priority among Lazarus developmemt team. 

I'm not at office and my home PC is not installed with Lazarus, so I cannot test it myself for now.

kito

  • Jr. Member
  • **
  • Posts: 79
Re: DBGrid EditingDone event
« Reply #4 on: February 07, 2021, 06:34:39 pm »
It is an invaluable idea that will definitely help me in my next projects and the pending once  :-[ , enables to reduce the search in the code path and determine the stage in which the error occurred.

  Going back to our basic problem, which was fixed by following your suggestion, I discovered that the code is called more than once, and to be frank, I am no longer comfortable using the same (editing done event)

before_post and before_scroll leads to an endless loop because I used it inside the function query.refresh

 I used instead as you suggested (query_ after_post ), and everything now goes smoothly after I have been going in circles for weeks  .

Thank you again for taking the time to respond and try to explain all this, I am very grateful to you.
« Last Edit: February 07, 2021, 07:10:05 pm by kito »
We can barely feel your presence, because of the depth of your silence

 

TinyPortal © 2005-2018