简体   繁体   中英

How Add pickup list to DBGrid at run time?

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;

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.

显示带有PickList的DBGrid图像

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.Columns 1 .PickList.add(slPickList[i]);//assigning end; end;

Below is the result:

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM