Recent

Author Topic: [SOLVED] Cannot set dbgrid selected column  (Read 498 times)

bpranoto

  • New Member
  • *
  • Posts: 42
[SOLVED] Cannot set dbgrid selected column
« on: September 08, 2019, 04:50:34 pm »
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:

Code: Pascal  [Select][+][-]
  1. procedure TForm1.ItemCodeChange(Sender: TField);
  2. begin
  3.   Self.BufDataset1.FieldByName('ITEM_NAME').AsString
  4.    := 'ITEM OF ' + Sender.AsString;
  5.  
  6.   {** Self.FKolQty is the column index of qty column (2 in this case). **}
  7.   Self.DBGrid1.SelectedIndex:=Self.FKolQty;
  8. end;
  9.  

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:
Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, BufDataset, db, Forms, Controls, Graphics, Dialogs,
  9.   DBGrids;
  10.  
  11. type
  12.  
  13.   { TForm1 }
  14.  
  15.   TForm1 = class(TForm)
  16.     BufDataset1: TBufDataset;
  17.     DataSource1: TDataSource;
  18.     DBGrid1: TDBGrid;
  19.   private
  20.     FKolQty : Integer;
  21.     procedure ItemCodeChange( Sender:TField);
  22.   public
  23.     constructor Create( AOwner:TComponent);override;
  24.  
  25.   end;
  26.  
  27. var
  28.   Form1: TForm1;
  29.  
  30. implementation
  31.  
  32. {$R *.lfm}
  33.  
  34. { TForm1 }
  35.  
  36. procedure TForm1.ItemCodeChange(Sender: TField);
  37. begin
  38.   Self.BufDataset1.FieldByName('ITEM_NAME').AsString
  39.    := 'ITEM OF ' + Sender.AsString;
  40.   Self.DBGrid1.SelectedIndex:=Self.FKolQty;  // <==== DOES NOT WORK
  41. end;
  42.  
  43. constructor TForm1.Create(AOwner: TComponent);
  44. var
  45.   F : TField;
  46.   Col : TColumn;
  47. begin
  48.   inherited Create(AOwner);
  49.   {***** create field dataset *******}
  50.   F := TStringField.Create(NIL);
  51.   F.FieldName:='ITEM_CODE';
  52.   F.FieldKind:=fkData;
  53.   F.Size:= 3;
  54.   F.DataSet := Self.BufDataset1;
  55.   F.OnChange:=@Self.ItemCodeChange;
  56.   //
  57.   F := TStringField.Create(NIL);
  58.   F.FieldName:='ITEM_NAME';
  59.   F.FieldKind:=fkData;
  60.   F.Size:= 20;
  61.   F.DataSet := Self.BufDataset1;
  62.   //
  63.   F := TIntegerField.Create(NIL);
  64.   F.FieldName:='QTY';
  65.   F.FieldKind:=fkData;
  66.   F.DataSet := Self.BufDataset1;
  67.  
  68.   //
  69.   Self.BufDataset1.CreateDataset;
  70.   Self.BufDataset1.Open;
  71.   Self.BufDataset1.Append;
  72.  
  73.   {**** setting grid column ******}
  74.   Col := Self.DBGrid1.Columns.Add;
  75.   Col.Title.Caption:= 'ITEM CODE';
  76.   Col.FieldName := 'ITEM_CODE';
  77.   //
  78.   Col := Self.DBGrid1.Columns.Add;
  79.   Col.Title.Caption:= 'ITEM NAME';
  80.   Col.FieldName := 'ITEM_NAME';
  81.   Col.ReadOnly:=True;
  82.   //
  83.   Col := Self.DBGrid1.Columns.Add;
  84.   Col.Title.Caption:= 'QTY';
  85.   Col.FieldName := 'QTY';
  86.   Col.Alignment:=taRightJustify;
  87.   Self.FKolQty:=Self.DBGrid1.Columns.Count - 1;
  88.   //
  89.  
  90. end;
  91.  
  92. end.
  93.  
  94.  

Attached is the demo project files.

Thanks for any pointer.
« Last Edit: September 08, 2019, 06:31:32 pm by bpranoto »

howardpc

  • Hero Member
  • *****
  • Posts: 3488
Re: Cannot set dbgrid selected column
« Reply #1 on: September 08, 2019, 05:38:21 pm »
Add a private method
Code: Pascal  [Select][+][-]
  1. procedure MoveToCol(aCol: PtrInt);
to your form, and change the field's OnChange handler to this:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.ItemCodeChange(Sender: TField);
  2. begin
  3.   BufDataset1.FieldByName('ITEM_NAME').AsString
  4.    := 'ITEM OF ' + Sender.AsString;
  5.   Application.QueueAsyncCall(@MoveToCol, FKolQty);
  6. end;
  7.  
  8. procedure TForm1.MoveToCol(aCol: PtrInt);
  9. begin
  10.   DBGrid1.SelectedIndex := aCol;
  11. end;

bpranoto

  • New Member
  • *
  • Posts: 42
[SOLVED[ Re: Cannot set dbgrid selected column
« Reply #2 on: September 08, 2019, 06:31:08 pm »
Add a private method
Code: Pascal  [Select][+][-]
  1. procedure MoveToCol(aCol: PtrInt);
to your form, and change the field's OnChange handler to this:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.ItemCodeChange(Sender: TField);
  2. begin
  3.   BufDataset1.FieldByName('ITEM_NAME').AsString
  4.    := 'ITEM OF ' + Sender.AsString;
  5.   Application.QueueAsyncCall(@MoveToCol, FKolQty);
  6. end;
  7.  
  8. procedure TForm1.MoveToCol(aCol: PtrInt);
  9. begin
  10.   DBGrid1.SelectedIndex := aCol;
  11. end;

The magic works!

Thank you

 

TinyPortal © 2005-2018