1
votes

i have a DBGrid and it is linked to client dataset when i assign a SQLQuery at run time the DBGrid automatically assigns no of column. What i need is when DBGrid automatically assign columns i need to set one of those columns to assign a picklist. can anyone help me? the following procedure calls in the forms on show event. the form contains DataSource, ClientDataSet, SQLViewQuery (TSQLQuery), DatasetProvider and DBGridDetails (TDBGrid).

procedure TViewDetailsForm.ViewPendingAndReturnCheques;
var I : Integer;
slPickList:TStringList;
begin
  slPickList := TStringList.Create;
  slPickList.Add('Pending');
  slPickList.Add('Returned');
  slPickList.Add('Passed');

  SQL := 'SELECT a.CHEQUE_NO, a.BANK, a.CHEQUE_DATE, a.AMOUNT,a.STATUS FROM CHEQUES a';

  //refreshisng the DBGrid
  SQLViewQuery.SQL.Clear;
  SQLViewQuery.SQL.Add(SQL);
  ClientDataSet.Active := false;
  ClientDataSet.Active := true;

  DBGridDetails.Columns[0].Width := _Block;
  DBGridDetails.Columns[1].Width := _Block;
  DBGridDetails.Columns[2].Width := _Block;
  DBGridDetails.Columns[3].Width := _Block;
  DBGridDetails.Columns[4].Width := _Block;


  for I := 0 to DBGridDetails.Columns.Count - 1 do
  begin
    if DBGridDetails.Columns[I].FieldName = 'STATUS' then
    begin
       DBGridDetails.Columns[i].ButtonStyle := cbsAuto;
       DBGridDetails.Columns[I].PickList := slPickList;
    end;
  end;

  Show;

end;
2
Have you seen this tutorial ?TLama
"but not works" is not helpful. If you've tried something, edit your question to include what you've tried, and explain how it doesn't work. You also need to define "picklist". Is it a list of hard-coded values ("Choice A, Choice B, Choice C"), or is it a list of values from another table?Ken White
i added my code _Block = 70 (Const)Azad

2 Answers

1
votes

Here's a sample app I just created in Delphi 2007 that demonstrates how to accomplish this. Here's all I did to set it up:

  1. Click File->New-VCL Forms Application from the IDE's main menu.
  2. Drop a TClientDataSet, a TDataSource, and a TDBGrid on the form.
  3. Click on the form, and then use the Object Inspector to create a new OnCreate event handler. Add the following code:

    procedure TForm1.FormCreate(Sender: TObject);
    var
      SL: TStringList;
    begin
      with ClientDataSet1 do
      begin
        FieldDefs.Clear;
        FieldDefs.Add('OrderNo', ftInteger);
        FieldDefs.Add('Status', ftString, 10);
        CreateDataSet;
      end;
      ClientDataSet1.Active := True;
    
      // Connect a datasource to the CDS
      DataSource1.DataSet := ClientDataSet1;
    
      // Connect the grid to that datasource to create the columns.
      DBGrid1.DataSource := DataSource1;
    
      // Create the picklist for the second column (Status)
      SL := TStringList.Create;
      try
        SL.Add('Pending');
        SL.Add('Returned');
        SL.Add('Passed');
        DBGrid1.Columns[1].ButtonStyle := cbsAuto;
        DBGrid1.Columns[1].PickList := SL;
      finally
        SL.Free;
      end;
    end;
    
  4. Run the application, click in the Status column in the grid, and you'll see the three choices added to the PickList above.

Image of DBGrid with PickList displayed

1
votes

You can assign values to the dbgrid column picklist during the run time.

Below is the code: procedure Tfrm1.FormShow(Sender: TObject); var slPickList:TStringList; I: Integer; begin slPickList := TStringList.Create; slPickList.Add('Pending'); slPickList.Add('Returned'); slPickList.Add('Passed'); for I := 0 to 2 do begin dbgViewAxiomClaims.Columns1.PickList.add(slPickList[i]);//assigning end; end;

Below is the result:

enter image description here