3

In my Delphi application, I use lookup fields, but in unusual way. Actually, I wanna update field in underlying data set, just like if it was in the same table.

Existing guides tell that there is no problem, just join the table and voila... I envy if they really succeeded this task with such simple solution. I do not. BTW I think I'm getting close to reach my goal. I have one question left: how the hell I can get value I just entered into DBGrid Cell?

I tried DBGrid[FieldName].EditValue and .DisplayText, but they show the same value as Field.Value, which doesn't change after exiting the column, because it is lookup field. Sender.NewValue is null. I'm using this function to update lookup table:

procedure TKDGridForm.LookupFieldChange(Sender: TField);
begin
  if not Assigned(Sender) then
    Exit;
  Sender.OnChange := nil;
  if not Assigned(Sender.LookupDataSet) then
    Exit;
  if Sender.LookupDataSet.Locate(Sender.LookupKeyFields, Sender.DataSet[Sender.KeyFields], []) then
    Sender.LookupDataSet.Edit
  else
    Sender.LookupDataSet.Append;
  // how do I get the value I just entered?
  Sender.Value := KDGrid3[Sender.FieldName].DisplayText;
  Sender.LookupDataSet.FieldValues[Sender.LookupResultField] := Sender.Value;
  Sender.LookupDataSet.Post;
  Sender.OnChange := LookupFieldChange;
end;

Here is SQL I used before I ended up with lookup fields:

select det.*, 
       od1.T_EQ T_SHABLON_EQ, 
       od1.T_NV T_SHABLON_NV, 
       od1.T_PRIM T_SHABLON_PRIM,
       od2.T_EQ T_PRAVKA_EQ, 
       od2.T_NV T_PRAVKA_NV, 
       od2.T_PRIM T_PRAVKA_PRIM,
       od3.T_EQ T_VALCOV_EQ, 
       od3.T_NV T_VALCOV_NV, 
       od3.T_PRIM T_VALCOV_PRIM,
       od4.T_EQ T_REZKA2_EQ, 
       od4.T_NV T_REZKA2_NV, 
       od4.T_PRIM T_REZKA2_PRIM
from CMKNEW.details det 
left join CMKNEW.OperDetails od1 
       ON det.nrec = od1.cdetail 
      and 81 = od1.coper 
left join CMKNEW.OperDetails od2 
       ON det.nrec = od2.cdetail 
      and 82 = od2.coper 
left join CMKNEW.OperDetails od3 
       ON det.nrec = od3.cdetail 
      and 83 = od3.coper 
left join CMKNEW.OperDetails od4 
       ON det.nrec = od4.cdetail 
      and 84 = od4.coper 
where det.ckd=:CKD order by det.NREC

Hope it will explain my task clearer. If you wanna mcve, I can extend this, though I think it's not essential.

My database is Oracle, connected through ADO. I'd like the solution to be as simple as possible.

1 Answer 1

2
+50

I assume you're talking about a standard TDBGrid and that what you're asking is how to get the text which is displayed in a cell of the grid when you type into it, but before the grid's dataset is updated. At that point, the current row indicator in the LH column will have changed from the default right-pointing triangle to an I-beam

If so, the snippet below shows you how to do get this text value. The point is, in the condition I've described, what's in the cell hasn't yet been posted back to the underlying dataset field. What happens is that when you start editing, an InplaceEditor (TCustomMaskEdit descendant) is dynamically created, and it's this which holds the text value which is being edited.

Add a TTimer and a TMemo to your form and then run the code below to see what I mean.

type
  TMyGrid = Class(TDBGrid);

procedure TMyForm.Timer1Timer(Sender: TObject);
var
  S : String;
  Grid : TmyGrid;
begin
  Grid := TmyGrid(DBGrid1);
  if Grid.InplaceEditor <> Nil then
    S := Grid.InplaceEditor.Text
  else
    S := IntToStr(Grid.Col) + ':' + IntToStr(Grid.Row);
  Grid.Invalidate;
  Memo1.Lines.Insert(0, S);
end;
3
  • Actually it works. And actually doesn't help me with X problem part, because OnFieldChange event is called in unpredictable manner :( I accept because it is the answer I asked about.
    – Danatela
    Commented Oct 15, 2015 at 10:39
  • Thanks. Why not try and explain the problem you're still having in a new q?
    – MartynA
    Commented Oct 15, 2015 at 10:43
  • It was very kind, generous and thoughtful of you to nominate this answer for a bounty. I appreciate it.
    – MartynA
    Commented Oct 26, 2015 at 18:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.