Einzelnen Beitrag anzeigen

Benutzerbild von Sir Rufo
Sir Rufo

Registriert seit: 5. Jan 2005
Ort: Stadthagen
9.454 Beiträge
 
Delphi 10 Seattle Enterprise
 
#2

AW: TDBComboBox mit mapped Values

  Alt 6. Jan 2015, 17:42
Hattest du an sowas gedacht?
Delphi-Quellcode:
unit Form.Main;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Data.DB, Datasnap.DBClient, Vcl.DBCtrls,
  Vcl.Grids, Vcl.DBGrids, Vcl.StdCtrls, Vcl.Mask, Vcl.ExtCtrls;

type
  TMainForm = class( TForm )
    GenderMappingData: TClientDataSet;
    PersonData: TClientDataSet;
    GenderMappingData_Gender: TIntegerField;
    GenderMappingData_DisplayName: TStringField;
    Person_Gender: TDBLookupComboBox;
    GenderMapping: TDataSource;
    PersonData_FirstName: TStringField;
    PersonData_LastName: TStringField;
    PersonData_Gender: TIntegerField;
    Person: TDataSource;
    PersonNavigator: TDBNavigator;
    Person_FirstName: TDBEdit;
    Person_LastName: TDBEdit;
    PersonGrid: TDBGrid;
    PersonData_GenderName: TStringField;
  private
    { Private-Deklarationen }
  public
    procedure AfterConstruction; override;

  end;

var
  MainForm: TMainForm;

implementation

{$R *.dfm}
{ TForm1 }

procedure TMainForm.AfterConstruction;
begin
  inherited;

  {GenderMappingData}
  GenderMappingData.CreateDataSet;
  GenderMappingData.LogChanges := False;
  GenderMappingData.AppendRecord( [1, 'Männlich'] );
  GenderMappingData.AppendRecord( [2, 'Weiblich'] );
  GenderMappingData.AppendRecord( [3, 'Intersex'] );
  GenderMappingData.AppendRecord( [4, 'Unbestimmt'] );

  {PersonData}
  PersonData.CreateDataSet;
  PersonData.LogChanges := False;
end;

end.
und mal die DFM dazu
Code:
object MainForm: TMainForm
  Left = 0
  Top = 0
  Caption = 'MainForm'
  ClientHeight = 232
  ClientWidth = 811
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'Tahoma'
  Font.Style = []
  OldCreateOrder = False
  PixelsPerInch = 96
  TextHeight = 13
  object Person_Gender: TDBLookupComboBox
    Left = 262
    Top = 176
    Width = 145
    Height = 21
    DataField = 'Gender'
    DataSource = Person
    KeyField = 'Gender'
    ListField = 'DisplayName'
    ListSource = GenderMapping
    TabOrder = 2
  end
  object PersonNavigator: TDBNavigator
    Left = 8
    Top = 8
    Width = 240
    Height = 25
    DataSource = Person
    TabOrder = 0
  end
  object Person_FirstName: TDBEdit
    Left = 8
    Top = 176
    Width = 121
    Height = 21
    DataField = 'FirstName'
    DataSource = Person
    TabOrder = 3
  end
  object Person_LastName: TDBEdit
    Left = 135
    Top = 176
    Width = 121
    Height = 21
    DataField = 'LastName'
    DataSource = Person
    TabOrder = 4
  end
  object PersonGrid: TDBGrid
    Left = 8
    Top = 39
    Width = 769
    Height = 120
    DataSource = Person
    TabOrder = 1
    TitleFont.Charset = DEFAULT_CHARSET
    TitleFont.Color = clWindowText
    TitleFont.Height = -11
    TitleFont.Name = 'Tahoma'
    TitleFont.Style = []
  end
  object GenderMappingData: TClientDataSet
    Aggregates = <>
    Params = <>
    Left = 648
    Top = 56
    object GenderMappingData_Gender: TIntegerField
      FieldName = 'Gender'
    end
    object GenderMappingData_DisplayName: TStringField
      FieldName = 'DisplayName'
      Size = 50
    end
  end
  object PersonData: TClientDataSet
    Aggregates = <>
    Params = <>
    Left = 648
    Top = 112
    object PersonData_FirstName: TStringField
      FieldName = 'FirstName'
      Size = 50
    end
    object PersonData_LastName: TStringField
      FieldName = 'LastName'
      Size = 50
    end
    object PersonData_Gender: TIntegerField
      FieldName = 'Gender'
      Visible = False
    end
    object PersonData_GenderName: TStringField
      FieldKind = fkLookup
      FieldName = 'GenderName'
      LookupDataSet = GenderMappingData
      LookupKeyFields = 'Gender'
      LookupResultField = 'DisplayName'
      KeyFields = 'Gender'
      Size = 50
      Lookup = True
    end
  end
  object GenderMapping: TDataSource
    AutoEdit = False
    DataSet = GenderMappingData
    Left = 504
    Top = 56
  end
  object Person: TDataSource
    DataSet = PersonData
    Left = 504
    Top = 112
  end
end
Angehängte Dateien
Dateityp: zip dp_183383.zip (1,8 KB, 5x aufgerufen)
Kaum macht man's richtig - schon funktioniert's
Zertifikat: Sir Rufo (Fingerprint: ‎ea 0a 4c 14 0d b6 3a a4 c1 c5 b9 dc 90 9d f0 e9 de 13 da 60)
  Mit Zitat antworten Zitat