RowChange / CellChanging

Good afternoon Bruno,

while moving between the row i check if the next selected row has the red color.

If yes, i need to skip it.



So in the rowchanging event i do (just for eample)



procedure TForm1.SG1RowChanging(Sender: TObject; OldRow, NewRow: Integer;

var Allow: Boolean);

begin

    If SG1.Colors[1,NewRow]=clRed then

    begin

        SG1.Row:=SG1.LastRow; // Last row is not red ...

    end;

end;



But instead go to last row, the cursor move to next row, even if is red.



So, i tried the same code into CellChanging event with same result.

Grid options, navigations and mouseactions as last message.



When i move the cursor the RowChange event is firedup 2 times, for this reason does not work properly.

the real procedure is



procedure TForm1.SG1RowChanging(Sender: TObject; OldRow, NewRow: Integer;

var Allow: Boolean);

var R : Integer;

begin

if SG1.Colors[1,NewRow]=clRed then

begin

    Allow:=False;

    if NewRow>OldRow then

    begin

      // Go Down ....

      if NewRow<SG1.LastRow then

      begin

        R:=NewRow;

        repeat

          R:=R + 1;

          if SG1.Colors[1,R]<>clRed then

          begin

            Allow:=True;

            SG1.Row:=R;

            Break

          end;

        until R=SG1.LastRow;

      end;

    end

    else

    begin

      // Go Up

      if NewRow>1 then

      begin

        R:=NewRow;

        repeat

          R:=R - 1;

          if SG1.Colors[1,R]<>clRed then

          begin

            Allow:=True;

            SG1.Row:=R;

            Break

          end;

        until R=1;

      end;

    end;

end;

end;



Excuse me for annoing you with the grid ...



Thank's for all



Daniele

I have retested this here with a basic sample:


procedure TForm1.AdvStringGrid1RowChanging(Sender: TObject; OldRow,
  NewRow: Integer; var Allow: Boolean);
begin
  if advstringgrid1.Colors[1,NewRow] = clRed then
  begin
    Allow := false;
    advstringgrid1.Row := AdvstringGrid1.RowCount - 1;
  end;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  advstringgrid1.Options := advstringgrid1.Options + [goRowSelect];
  advstringgrid1.Colors[1,3] := clRed;
  advstringgrid1.Colors[1,5] := clRed;
end;

but this way, the last row does get selected here when wanting to select a red row.
Do you have other grid settings / event handlers that might interfere.

Good morning Bruno,

thank's for your reply.

After some tests, i see that the problem is the procedure itself (meaning tha way i use the procedure).

Grid1RowChanging(Sender: TObject; OldRow, NewRow: Integer;

var Allow: Boolean); is called more than one time. In this way NewRow have always the original value. when run, the first time the procedure is called the row is setted as expected but, in the secon time the OldRow store the new row value and NewRow store the original one. This can be obtained using the procedure i posted above.

In fact, the red rows are busy and need to skip them, if i have 2 or more red rows contiguous i need to find the white one free.

For example i change row from 2 to 3, so we have oldrow=2, newrow=3.

But 3,4,5 are red, the next one "free" is 6.

In the procedure, 3>2 go down in the grid and SG1.Row:=6 (after all check).

when setted the new value (6), the onRowChange is called again but now with OldRow:=6 (new value) and NewRow=3 (old original value).. Here, 3<6 ... go up in the grid. The row is not changing.



Thank's for all Bruno.



Daniele