-2

Is there a way I can achieve cxGrid's functionality "go to next cell on enter" but using arrow keys on the keyboard instead of hitting "enter" ???

Right now I can navigate with the arrow keys but the cells I move to are not selected (blue color). If I edit a cell in the column and move to the cell beneath it (with the arrow key) , the grid jumps to some other random record in the grid,which is annoying.

Is it a question of settings or must you program this functionality ?

edit : This is a simple temporary table that I use to gather data. It has no indexed fields or autoinc field :

procedure TForm3.cxButton1Click(Sender: TObject);
begin
DataModule2.ACRQuery2.Close;
DataModule2.ACRQuery2.SQL.Clear;
DataModule2.ACRQuery2.SQL.Text :='insert into TEMP select company_id,member_id,surname,name from MEMBERS where company_id =:a1';
DataModule2.ACRQuery2.Params.ParamByName('a1').Value :=cxLookupComboBox2.Text;
DataModule2.ACRQuery2.ExecSQL;

DataModule2.ACRQuery3.Close;
DataModule2.ACRQuery3.SQL.Clear;
DataModule2.ACRQuery3.SQL.Text :='update temp set month=:a1,year=:a2';
DataModule2.ACRQuery3.Params.ParamByName('a1').Value :=cxComboBox2.Text;
DataModule2.ACRQuery3.Params.ParamByName('a2').Value :=cxTextEdit2.Text;
DataModule2.ACRQuery3.ExecSQL;

DataModule2.TEMP.Refresh;
end;

Data loads but when i edit the first record and move with the arrow down, the cursor jumps to the end of the grid .

enter image description here

Navigation with the arrow key functions normally.I can scroll down the column without problem and edit any record I pause on. But as soon as I edit it and move to the next record, the cursor jumps somwhere else. It seems the sync does not function. I really dont know. I am not sorting in any way .

The temp table : enter image description here

user763539
  • 3,509
  • 6
  • 44
  • 103
  • 2
    Have you tried asking in the DevEx support forums? – Ken White Jun 11 '16 at 00:51
  • 1
    On reflection, until this q is edited to include an MCVE, I'm voting to close it as off topic, because the "jumping" is not default behaviour of a cxGrid **and** the arrow-key navigation is the default for a cxGrid. So the q should contain sufficient info to reproduce the problem. – MartynA Jun 11 '16 at 14:36
  • @MartynA - sort order is none. – user763539 Jun 12 '16 at 01:47
  • Are Month, Year and Membership the only columns in your grid? – MartynA Jun 13 '16 at 17:50
  • no, a few more...name,surname and 2 calculated fields. – user763539 Jun 13 '16 at 19:33
  • Another query: the Insert carried out by your ACRQuery2 does not include a Month or Year column, nor any kind of data field. So, how come your ACRQuery3 manages to update temp's month and year columns, as they don't exist in your temp table? – MartynA Jun 13 '16 at 19:36
  • What's the answer to my query about the Month and Year columns, then? – MartynA Jun 15 '16 at 21:24

2 Answers2

2

Is it a question of settings or must you program this functionality ?

Yes, it is a question of settings and it doesn't require any code. However, there is evidently a problem in your project which I think you need to sort out first.

When you say "the grid jumps to some other random record in the grid,which is annoying.", it sounds as if you are starting from the the wrong place to try and get the navigation behaviour you want because jumping like that certainly shouldn't happen. And it doesn't in any cxGrid project of mine.

Anyway, I find that the representation of a cxGridDBTableView in the Object Inspector sometimes makes it hard to "see the wood for the trees". What I do then is to use a project which creates the grid entirely in code - see below.

The code below is completely self-contained and doesn't require any event handlers, persistent TFields, etc. If you try it, you should find that by default, as created, the grid does support up and down and left and right cell navigation using the cursor keys. The only exception is that the left and right keys don't work for cell navigation when the current cell contents are being edited. However, if you uncomment the line

  cxView.DataController.Options := cxView.DataController.Options + [dcoImmediatePost];

then, pressing Enter while editing in a cell immediately posts the edit back to the dataset, and the grid will allow you to navigate out of the cell by using the left- or right-arrow key. I'm sure there are fancier ways of achieving this effect by processing the key handling in code, but at least the dcoImmediatePost method has the advantage of requiring no code.

When the app starts, you should see the see the top row "highlighted" (colored blue by default) except for its LH cell, which is focused.

Hopefully, this example will help you identify the cause of the "jumping" in your project and may also help you refine your description of what you would like to obtain, in terms of cursor-key navigation.

Code

procedure TForm1.CreateGrid;
begin
  cxGrid := TcxGrid.Create(Self);
  cxGrid.Parent := Self;
  cxGrid.Width := 400;

  cxLevel := cxGrid.Levels.Add;
  cxLevel.Name := 'Firstlevel';

  cxView := cxGrid.CreateView(TcxGridDBTableView) as TcxGridDBTableView;
  cxView.Name := 'ATableView';
  //  Uncomment the following line to make the grid respond to the `Enter` key by posting any pending change to the data row
  //    cxView.DataController.Options := cxView.DataController.Options + [dcoImmediatePost];

  cxView.DataController.KeyFieldNames := 'ID';

  cxLevel.GridView := cxView;

  cxView.DataController.DataSource := DS1;

  cxView.DataController.CreateAllItems;

end;

function CreateField(AFieldClass : TFieldClass; AOwner : TComponent; ADataSet : TDataSet;
AFieldName, AName : String; ASize : Integer; AFieldKind : TFieldKind) : TField;
begin
  Result := AFieldClass.Create(AOwner);
  Result.FieldKind := AFieldKind;
  Result.FieldName := AFieldName;
  Result.Name := AName;
  Result.Size := ASize;
  Result.DataSet := ADataSet;
end;

procedure TForm1.FormCreate(Sender: TObject);
var
  i : Integer;
  Field : TField;
begin

  Field := CreateField(TAutoIncField, Self, CDS1, 'ID', 'CDS1ID', 0, fkData);
  Field := CreateField(TBooleanField, Self, CDS1, 'Marked', 'CDS1Marked', 0, fkData);
  Field := CreateField(TStringField, Self, CDS1, 'Name', 'CDS1Namefield', 20, fkData);
  Field := CreateField(TStringField, Self, CDS1, 'Value', 'CDS1Valuefield', 20, fkData);

  CDS1.CreateDataSet;

  CDS1.IndexFieldNames := 'ID';

  for i := 1 to 5 do begin
    CDS1.Insert;
    CDS1.FieldByName('Marked').AsBoolean := Odd(i);
    CDs1.FieldByName('Name').AsString := 'Name' + IntToStr(i);
    CDs1.FieldByName('Value').AsString := 'Value  ' + IntToStr(i);
    CDS1.Post;
  end;

  CDS1.First;

  CreateGrid;

  ActiveControl := cxGrid;

end;
MartynA
  • 30,454
  • 4
  • 32
  • 73
  • well, in my grid the 'jump' occurs. If I edit the first cell of a column and move with the arrow down to the next cell bellow, the grid switches focus to the tenth cell under skipping 9 rows. This happens only when editing. The navigation by itself works. I would like to have like Excel behaviour. – user763539 Jun 11 '16 at 13:58
  • Well, it obviously shouldn't jump like that, and my example project proves that jumps don't happen by default. You need to find the cause of that yourself. F.i. when you do an edit which causes a jump, is the edit changing a field your dataset it indexed upon? Btw, according to SO rules when you post a "why isn't this working?" question, you are supposed to include a MCVE in your question. – MartynA Jun 11 '16 at 14:22
  • "You need to find the cause " Perhaps you have a grid column whose SortOrder is set to soAscending or soDescending? – MartynA Jun 11 '16 at 14:33
0

After countless experiments I think I have found the issue : It seem that to use this functionality the data controller must be set to "Grid mode".

user763539
  • 3,509
  • 6
  • 44
  • 103
  • Sorry, as a general proposition, the idea that GridMode needs to be True to get arrow-key navigation is simply wrong. GridMode is False by default. So if setting it to True fixes your problem, the cause is something you have not told us in your q. – MartynA Jun 12 '16 at 07:52
  • I have been reading devexpress documentation these days .From them : " ExpressDataController working in default data loading mode sorts only its records, while the record order of the underlying dataset is not affected. This allows our controls to perform better with large datasets. This, however, means that the orders of the Data Controller's and the dataset's records are not longer synchronized and thus, you observe the described effect - cursor "jumps" ... It seems that the sync mode is not functioning properly. – user763539 Jun 13 '16 at 16:38
  • Ok, as that's apparently what Devex have said, I've retracted my vote-to-close. I've never encountered this jumping myself, btw in 20 years of using the cxGrid and its precursors. It's still a pity you haven't provided an MCVE, though. – MartynA Jun 13 '16 at 16:42
  • Btw, you quote Devex saying "This, however, means that the orders of the Data Controller's and the dataset's records are not longer synchronized". But in another comment you said "sort order is none". Well, if that's true how are you managing to obtain a different record order in your grid than the dataset's record order? – MartynA Jun 13 '16 at 17:03