Hi,
Let's say I have 3 fields in the table: ITEM_CODE,ITEM_NAME and QTY.
This table presented to the user in a dbgrid with coresponding columns of the fields.
The user edit the record directly in the dbgrid. After the user enters the item code in the grid, I want to change the active cell in column Qty. I code this in the OnChange event of ITEM_CODE field like this:
procedure TForm1.ItemCodeChange(Sender: TField);
begin
Self.BufDataset1.FieldByName('ITEM_NAME').AsString
:= 'ITEM OF ' + Sender.AsString;
{** Self.FKolQty is the column index of qty column (2 in this case). **}
Self.DBGrid1.SelectedIndex:=Self.FKolQty;
end;
However, it doesn't act like I want, after the OnChange event the active cell column is in column Item name.
Here is the full source code:
unit Unit1;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, BufDataset, db, Forms, Controls, Graphics, Dialogs,
DBGrids;
type
{ TForm1 }
TForm1 = class(TForm)
BufDataset1: TBufDataset;
DataSource1: TDataSource;
DBGrid1: TDBGrid;
private
FKolQty : Integer;
procedure ItemCodeChange( Sender:TField);
public
constructor Create( AOwner:TComponent);override;
end;
var
Form1: TForm1;
implementation
{$R *.lfm}
{ TForm1 }
procedure TForm1.ItemCodeChange(Sender: TField);
begin
Self.BufDataset1.FieldByName('ITEM_NAME').AsString
:= 'ITEM OF ' + Sender.AsString;
Self.DBGrid1.SelectedIndex:=Self.FKolQty; // <==== DOES NOT WORK
end;
constructor TForm1.Create(AOwner: TComponent);
var
F : TField;
Col : TColumn;
begin
inherited Create(AOwner);
{***** create field dataset *******}
F := TStringField.Create(NIL);
F.FieldName:='ITEM_CODE';
F.FieldKind:=fkData;
F.Size:= 3;
F.DataSet := Self.BufDataset1;
F.OnChange:=@Self.ItemCodeChange;
//
F := TStringField.Create(NIL);
F.FieldName:='ITEM_NAME';
F.FieldKind:=fkData;
F.Size:= 20;
F.DataSet := Self.BufDataset1;
//
F := TIntegerField.Create(NIL);
F.FieldName:='QTY';
F.FieldKind:=fkData;
F.DataSet := Self.BufDataset1;
//
Self.BufDataset1.CreateDataset;
Self.BufDataset1.Open;
Self.BufDataset1.Append;
{**** setting grid column ******}
Col := Self.DBGrid1.Columns.Add;
Col.Title.Caption:= 'ITEM CODE';
Col.FieldName := 'ITEM_CODE';
//
Col := Self.DBGrid1.Columns.Add;
Col.Title.Caption:= 'ITEM NAME';
Col.FieldName := 'ITEM_NAME';
Col.ReadOnly:=True;
//
Col := Self.DBGrid1.Columns.Add;
Col.Title.Caption:= 'QTY';
Col.FieldName := 'QTY';
Col.Alignment:=taRightJustify;
Self.FKolQty:=Self.DBGrid1.Columns.Count - 1;
//
end;
end.
Attached is the demo project files.
Thanks for any pointer.