Question

I have a table with two columns. ConfigItem and ConfigValue. Now I want to populate this in a dbgrid where ConfigValue should be a dbcombobox.

Sample ConfigItem(First Column)

Product

Product Type

Item Type

Items

ConfigValue should have a dbcombobox and the items of the combobox should be populated on the basis of the values in the first column.

Example. If user clicks on the first row which has Product as config Item then for the same row ConfigValue column in the grid should contain combobox with list of Products.

Possibly I can use BeforeDrawCell event of grid however I am trying to find a way by which this can be handled using adoquery or dataset component.

Could someone please guide on the solution approach tho this problem.

Thanks in advance, Divyesh

Was it helpful?

Solution

You can use the AfterScrollEvent to assign a PickList to your Column.
Picklist are here Stringlists assigned to the object of a master StringList. Depdending on your Delphi version you could use a generic dictionary.

unit Unit2;
interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, DB, Grids, DBGrids, ADODB;

type
  TForm2 = class(TForm)
    ADODataSet1: TADODataSet;
    DBGrid1: TDBGrid;
    DataSource1: TDataSource;
    procedure FormCreate(Sender: TObject);
    procedure ADODataSet1AfterScroll(DataSet: TDataSet);
  private
    { Private-Deklarationen }
    FList: TStringList;
  public
    { Public-Deklarationen }
  end;

var
  Form2: TForm2;

implementation

{$R *.dfm}

procedure TForm2.ADODataSet1AfterScroll(DataSet: TDataSet);
var
  idx: Integer;
begin
  if Assigned(FList) and (DBGrid1.Columns.Count > 1) then
  begin
    DBGrid1.Columns[1].ButtonStyle := cbsAuto;
    idx := FList.IndexOf(DBGrid1.Columns[0].Field.asString);
    if idx > -1 then
      DBGrid1.Columns[1].PickList := TStringList(FList.Objects[idx])
    else
      DBGrid1.Columns[1].PickList := nil;
  end;
end;

procedure TForm2.FormCreate(Sender: TObject);
var
  i: Integer;
begin  // some demo filling
  FList := TStringList.Create;
  FList.AddObject('A1A1', TStringList.Create);
  for i := 0 to 10 do
    TStringList(FList.Objects[FList.Count - 1]).Add(Format('group1_%d', [i]));
  FList.AddObject('A1A2', TStringList.Create);
  for i := 0 to 10 do
    TStringList(FList.Objects[FList.Count - 1]).Add(Format('group2_%d', [i]));
end;

procedure TForm2.FormDestroy(Sender: TObject);
var
  i: Integer;
begin
  for I := 0 to FList.Count - 1 do Flist.Objects[i].Free;
  FList.Free;
end;

end.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top