0
votes

I wasted a bit of time trying to work out something I figured would be simple.

I've got a database with multiple tables (MySQL). One table containing "Components" and another containing "Products". Products are built using Components, for example; Product ABC might be made up of 3 x Screws, 4 x bolts, 1 kilogram of fresh air... etc! Am I making sense so far?

The components are displayed in a DBGrid. If the user makes a mistake and wants to add another "Component" to a "Product" a Picklist appears listing all Components (from a different table) for them to select from.

Now, here's my problem! When something is selected from the column[i].picklist (this is part of a DBGrid) how do I know what was selected. I thought there would be an event fired, but there doesn't seem to be.

I need to know which item was selected so I can retrieve an appropriate description for the next field.

There are 3 fields, they are COMPONENT, DESCRIPTION, QUANTITY. Only COMPONENT and QUANTITY can be edited by the user.

I hope I'm making some sense here.

Here is the code I'm using now (as messy as it is);

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

1 Answers

-1
votes

This will show the data of the selected rows of the 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;