在columns.picklist中选择了什么?

时间:2016-05-09 11:23:37

标签: database delphi dbgrid

我浪费了一些时间试图找出一些我认为很简单的东西。

我有一个包含多个表(MySQL)的数据库。一个表包含"组件"另一个包含"产品"。例如,产品使用组件构建;产品ABC可能由3个螺丝,4个螺栓,1千克新鲜空气等组成!到目前为止,我有意义吗?

组件显示在DBGrid中。如果用户犯了错误并想要添加另一个"组件"到"产品"出现一个选择列表,列出所有组件(来自不同的表),供他们选择。

现在,这是我的问题!当从列[i] .picklist(这是DBGrid的一部分)中选择某些内容时,我如何知道所选内容。我以为会有一个事件被解雇,但似乎没有。

我需要知道选择了哪个项目,以便我可以为下一个字段检索适当的描述。

有3个字段,它们是COMPONENT,DESCRIPTION,QUANTITY。用户只能编辑COMPONENT和QUANTITY。

我希望我在这里有所作为。

以下是我现在使用的代码(虽然很乱);

procedure TForm1.CompletePolesSourceStateChange(Sender: TObject);
var
  loop: Integer;
  Tmp: string;
begin

  case CompletePolesSource.state of
    dsInsert:
      begin
        CompVals.Clear;  // Is a tstringlist created elsewhere
        CompVals.Delimiter := '|';
        CompVals.QuoteChar := '"';

        PoleComponentsGrid.Columns[0].readonly := false; // Is readonly when not in DSInsert

        PoleComponentsGrid.Columns[0].PickList.Clear;  // Clear other crap

        {
          Now add Parts to a Name / Value list (CODE / DESCRIPTION) so I can later
          get the description without looking it up in the other table.
        }

        for loop := 1 to componentstable.RecordCount do  // Get CODE from other table
        begin
          componentstable.RecNo := loop;
          tmp := componentstable.Fieldbyname('CODE').asstring + '=' + componentstable.Fieldbyname('ITEM').asstring;
          CompVals.Add(tmp);
          PoleComponentsGrid.Columns.Items[0].PickList.Add(tmp);
        end;

        PoleComponentsGrid.Columns.Items[0].readonly := true;

      end;
  end;
end;

1 个答案:

答案 0 :(得分:-1)

这将显示DBGrid的所选行的数据

procedure TFrmPrincipal.btnShowSelectedRowsClick(Sender: TObject);
var
    i: Integer;
    aux: string;
begin
    for i := 0 to DBGrid1.SelectedRows.Count - 1 do
    begin
        ClientDataSet1.GotoBookmark(pointer(DBGrid1.SelectedRows.Items[i]));
        aux := aux + IntToStr(ClientDataSet1.RecNo) + ' - ' +
        ClientDataSet1.FieldByName('CUSTOMER').AsString + #13;
    end;
    ShowMessage(‘Selected Rows: ‘ + #13 + aux);
end;