[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Focus on First Cell Insert Row after Post in Delphi cxGrid

Posted on 2012-08-16
13
Medium Priority
?
6,823 Views
Last Modified: 2012-09-19
After user posts a record (on Return or Tab), how do I set focus to the first cell of the Insert Row?

I want users to be able to press <enter> to post the record and begin typing the next record in without the need for mouse clicks to <click> on the Insert New Record row.

Thanks!

  //post instruction information
  with dmWorkorder.qryInstructions do begin
    if (State = dsInsert) or (State = dsEdit) then begin
        dmWorkorder.qryInstructions.Edit;
        dmWorkorder.qryInstructions.FieldValues['wonum'] := edWoNumber.Text;
        Post;
    end;
  end;

Open in new window

0
Comment
Question by:Bianca
  • 5
  • 5
  • 3
13 Comments
 
LVL 38

Expert Comment

by:Geert Gruwez
ID: 38304116
have you tried the arrow down key (post when moving to next row)
and then use the insert key, the default for adding a new row
0
 
LVL 28

Accepted Solution

by:
Sinisa Vuk earned 1000 total points
ID: 38304453
Take a look at my example. Main goal is to use appropriate event.

...
procedure TForm1.ADOTable1AfterPost(DataSet: TDataSet);
begin
  DataSet.Append;
end;

procedure TForm1.ADOTable1BeforeClose(DataSet: TDataSet);
begin
  if (DataSet.State = dsInsert) or (DataSet.State = dsEdit) then
  begin
    DataSet.Cancel;
  end;
end;

procedure TForm1.ADOTable1AfterInsert(DataSet: TDataSet);
begin
  DBGrid1.SelectedField := ADOTable1.Fields.Fields[0];
end;

procedure TForm1.FormShow(Sender: TObject);
begin
  if (ADOTable1.State = dsBrowse) then
  begin
    ADOTable1.Append;
  end;
end;

procedure TForm1.DBGrid1KeyPress(Sender: TObject; var Key: Char);
begin
  if Key = #13 then
  begin
    if (ADOTable1.State = dsInsert) or (ADOTable1.State = dsEdit) then
    begin
      ADOTable1.Post;
    end;
  end;
end;
...

Open in new window

0
 
LVL 38

Expert Comment

by:Geert Gruwez
ID: 38304877
looks like you check if the dataset is in edit mode
and then set it in edit mode ... a little odd

with a cxGrid, you can use the views datacontroller to change the data

with viewInstructions.DataController do
begin
  BeginUpdate;
  try
    values[FocusedRecordIndex, columnWoNum.Index] := edWoNumber.Text;
  finally
    EndUpdate;
  end;
end;


always wrap changes to the grid/view in begin and endupdate

to end the editing mode of the grid close the editor (and accept it's current value)
if viewInstructions.Controller.IsEditing then
  viewInstructions.Controller.EditingController.HideEdit(True);
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:Bianca
ID: 38305046
Thanks Geert.. I was informally trained in a rush setting so I made do with what I had. I have never used BeginUpdate/EndUpdate when posting to grids/views. You just presented me with about 4 weeks of work switching out all my post code!  :)

I thought about calling Insert key but for the life of me I cannot remember how to do it. Please refresh my memory. Thanks!
0
 
LVL 38

Expert Comment

by:Geert Gruwez
ID: 38310843
dev ex grid editing isn't simple
it took me a few years to get good at it too ... and then they let me become oracle dba

for the insert key, you don't need to do anything
just click in the grid and hit insert key,
it will add a new row in the grid and put the cursor on the first column in edit mode

if you set the newitemrow.visible := true in the grid view, the new row will always be in the same place: at the top

4 weeks of work switching code ???
it depends what you have now and how it's working now.
sometimes you can find a mix between my code and sinisav's code is enough to do the trick
0
 

Author Comment

by:Bianca
ID: 38318601
Can I 'press' the Insert Key for the user instead? Right after the record posts, I want user to just begin typing immediately to begin the next new record, not having to press 'Insert' each time.
0
 
LVL 38

Expert Comment

by:Geert Gruwez
ID: 38319322
that's odd behaviour

have you considered it annoying if the user only wants to insert 1 record
and then at the end has to delete the unwanted next record ?

it's possible with sending VK_INSERT
http://delphi.about.com/od/adptips2004/a/bltip0604_4.htm
0
 
LVL 28

Expert Comment

by:Sinisa Vuk
ID: 38319338
Have you try my example? There is auto insert record already done.
0
 

Author Comment

by:Bianca
ID: 38320975
@sinisav
I don't have SelectedField as a property available for my grid view's dataset. Maybe that is my initial problem? I have the gridview linked to an ADOQuery linked to a TDataSource.
0
 

Author Comment

by:Bianca
ID: 38320992
@Geert
it is kind of like a user typing up a grocery list or a to-do list. I want them to type, press enter, then focuses to insert new record row so they can continue to type. If they are done, user presses Esc which cancels out the Insert.
0
 

Author Comment

by:Bianca
ID: 38321010
I just tried the OnPost Event on my qryInstructions and it did not do anything.

frmWoDataEntry.gvInstructions.Controller.FocusedRowIndex := -1;

Open in new window

0
 
LVL 28

Expert Comment

by:Sinisa Vuk
ID: 38324744
Sorry, my code is for delphi's old dbgrid.

try to set AfterInsert event as:

...
frmWoDataEntry.gvInstructions.SetFocus;
frmWoDataEntry.gvInstructions.Controller.FocusedRowIndex := frmWoDataEntry.gvInstructions.DataController.RecordCount - 1;
frmWoDataEntry.gvInstructions.Controller.EditingController.ShowEdit(frmWoDataEntry.gvInstructions.Columns[0]);
...

Open in new window

0
 
LVL 38

Expert Comment

by:Geert Gruwez
ID: 38324956
ow ... why didn't you tell me this was for a grocerylist
i finally understand ... loool

here's how i was able to enter data using only the enter key ...
in the view properties OptionsBehaviour:
FocusCellOnCycle := True;
FocusFirstCellOnNewRecord := True;
GoToNextCellOnEnter := True;

in the OnKeyUp, assign following code:
procedure TForm1.viewDataEditKeyUp(Sender: TcxCustomGridTableView;
  AItem: TcxCustomGridTableItem; AEdit: TcxCustomEdit; var Key: Word;
  Shift: TShiftState);
begin
  if (aItem = colData3) and (Key = VK_RETURN) then // if last column and enter hit
    PostMessage(Handle, WM_USER+2, 0,0);
end;

Open in new window


in the form:
const
  W_NEWROW = WM_USER+2;

type
  TForm1 = class(TForm)
    lvlSetup: TcxGridLevel;
    gridMain: TcxGrid;
    viewData: TcxGridTableView;
    colData1: TcxGridColumn;
    colData2: TcxGridColumn;
    colData3: TcxGridColumn;
    procedure viewDataKeyUp(Sender: TcxCustomGridTableView;
      AItem: TcxCustomGridTableItem; AEdit: TcxCustomEdit; var Key: Word;
      Shift: TShiftState);
  private
    procedure NewRowData(var Msg: TMessage); message W_NEWROW;

procedure TForm1.NewRowData(var Msg: TMessage);
begin
  viewData.Controller.EditingController.HideEdit(True);
  viewData.DataController.Post(True);
  viewData.DataController.Append;
  viewData.Controller.FocusedColumn := colData3;
end;

Open in new window

0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question