Link to home
Avatar of SarikaKev
SarikaKev

asked on

Update DBGRID Column onDataChange

Hi,
I have a dbgrid with columns.
when i change a value in column 1 I need the column to which is a Y/N lookuplist to change automatically to Y.
eg   COL1 NUM     COL2 CALC
       1234             N                         if i change the NUM field I need CALC to change to Y
ie     4567            "Y"

thes values must be written to the DB.
GRID NAME = dgbView

Please help.  thanks
Avatar of Limbeck
Limbeck

something like this:(blabla & sumthing are to be replaced with the fieldnames). in the oncolexit event of the grid

procedure TForm1.dgbViewColExit(Sender: TObject);
  function getval(i:integer):string;
  begin
    if i>4 then result:='N' else result:='J' ;
  end;

begin
  with dgbView.datasource.DataSet do
  if state=dsedit
    then if fieldbyname('blabla').value<>fieldbyname('blabla').OldValue
      then fieldbyname('sumthing').AsString=getval(Fieldbyname(' blabla').AsInteger);
  if state=dsinsert
    then if fieldbyname('blabla').value<>fieldbyname('blabla').OldValue
        then fieldbyname('sumthing').AsString=getval(Fieldbyname(' blabla').AsInteger);

end;
ugh

procedure TForm1.dgbViewColExit(Sender: TObject);
  function getval(i:integer):string;
  begin
    if i>4 then result:='N' else result:='J' ;
  end;

begin
  with dgbView.datasource.DataSet do
  if state=dsedit
    then if fieldbyname('blabla').value<>fieldbyname('blabla').OldValue
      then fieldbyname('sumthing').AsString=getval(Fieldbyname('blabla').AsInteger);
  if state=dsinsert
        then fieldbyname('sumthing').AsString=getval(Fieldbyname('blabla').AsInteger);

end;
ASKER CERTIFIED SOLUTION
Avatar of Limbeck
Limbeck

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
and

AsString=getval should be :=

i shouldnt type nd talk on the phone at the same time ;)
Avatar of SarikaKev

ASKER

Hi Limbeck... thx for reponse
but what is teh purpose of  :


   ///////////////////    if i>4 then result:='N' else result:='J' ;////////////////////


also i assume i wont need  if state=dsinsert
        then fieldbyname('sumthing').AsString=getval(Fieldbyname('blabla').AsInteger);
  end;
because the use cannot add records..only edit existing records.

will test this now and get back to u .. thx so much


oh ok, assumed that tif the value was 1,2,3 or 4 the result should be 'Y', if not 'N'

well the code is a start, you can take it from there :)

and the insert, sure, if you dont need it it can be removed
Why don't you use the events of the dataset?
onAfterPost place this code:

dataset.Edit;
dataset.FieldByName('col2').AsString:='y';
because if you move from col 1 to col b it doesnt post :)
No comment has been added to this question in more than 21 days, so it is now classified as abandoned.

I will leave the following recommendation for this question in the Cleanup Zone:
Accept Limbeck's comment as answer {http:#17478707}.

Any objections should be posted here in the next 4 days. After that time, the question will be closed.

ciuly
Experts Exchange Cleanup Volunteer
wow this is an old 1 :) thank you for taking the time ciuly.