Users input the code in a tdbgrid with has a dataset, but this inputted code is not really change the value of the table, because this code is not saved in the table.
Mmm. Then why is that a dataset if the user-input isn't going to be saved?
Or is the T_Detail a list of possible values in M_Gd and the user SELECTS one of the records in T_Detail???
In that case it's a whole other story.
You have a few possible coices.
1) You can save the value in OnEnter and check it each time. When you do a search you can reset the old value to the just searched text. That would be easiest to implement in your current code.
2) If the T_Detail is a set of possible key values of M_Gd (in that case it's a really strange name because normaly it's the other way around) then you can make a Master-Detail relation with 2 DBGrids. In that case, when you scroll though DB1, the records matching in DB2 will be displayed automatically. Although in that case DB2 will only show that record(s) and not all other records.
3) You can create the master-detail element yourself by using DB1.OnScroll and automatically search/locate the correct record in DB2. In that case the searching is instant (during scrolling of DB1). That could be slow depending on you datasets (so you need to figure out if that's what you want).
But thinking of it... what exactly is the problem at this moment? If you have typed a value in the first grid, and you searched it in the second... you can just check if you are already on that record, can't you?
procedure TFrm.GrdDtKeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
begin
if Key = 13 then
begin
if GrdDt.SelectedField.Name ='Dt2CD' then
begin
GrdDt.SelectedField := Dt2QTY;
if Dt2CD.NewValue <> VALUE_FROM_DB2_RECORD then
begin
YOU NEED TO SEARCH
end;
end;
end;
end;