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
Delphi

Avatar of undefined
Last Comment
Limbeck

8/22/2022 - Mon
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;
Limbeck

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
Limbeck

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Limbeck

and

AsString=getval should be :=

i shouldnt type nd talk on the phone at the same time ;)
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
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


Limbeck

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
calinutz

Why don't you use the events of the dataset?
onAfterPost place this code:

dataset.Edit;
dataset.FieldByName('col2').AsString:='y';
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
Limbeck

because if you move from col 1 to col b it doesnt post :)
2266180

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
Limbeck

wow this is an old 1 :) thank you for taking the time ciuly.
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes