Difference between revisions of "TDBLookupComboBox/fr"
Line 3: | Line 3: | ||
'''TDBLookupComboBox''' [[image:tdblookupcombobox.png]] est un [[TComboBox/fr|TComboBox]] lié aux données (doublement). Il peut être trouvé sur l'[[Data Controls tab/fr|onglet Data Controls]] de la [[Component Palette/fr|palette de composants]]. | '''TDBLookupComboBox''' [[image:tdblookupcombobox.png]] est un [[TComboBox/fr|TComboBox]] lié aux données (doublement). Il peut être trouvé sur l'[[Data Controls tab/fr|onglet Data Controls]] de la [[Component Palette/fr|palette de composants]]. | ||
− | + | Le contrôle TDBLookupCombobox obtient une liste de valeur depuis sa ''ListSource'' qui représente la donnée d'une autre table (p.ex. des produits). Alors, il | |
− | * | + | * affiche les valeurs dans la '''ListField''' (p.ex. le champ "NomDeProduit") |
− | * | + | * se rappelle des valeurs dans la propriété '''KeyField''' (p.ex. un champ "ID") |
− | + | Le ComboBox stocke le résultat (i.e. la valeur '''KeyField''') dans la propriété '''DataField''', p.ex. le champ "ProduitID" dans la propriété '''DataSource''' (p.ex. une table "Commandes"). | |
− | + | La différence avec le [[TDBComboBox/fr|TDBComboBox]] est que le TDBComboBox est un "contrôle individuellement lié aux données" (en quelque sorte). | |
==Utilisation sans liaison== | ==Utilisation sans liaison== | ||
− | + | Vous pouvez obtenir la combobox pour rechercher des valeurs d'une table sans enregistrer les résultats dans une autre en laissant les propriétés ''DataSource'' et ''KeyField'' 'vides. | |
− | + | L'extrait de code exemple suivant : | |
− | * | + | * permettra à un TDBLookupComboBox d'être configuré en mode non lié (i.e. sans aucun changement porté dans la base de données) |
− | * Ensure the visible data in the TDBLookupComboBox is from the selected record in the dataset (i.e. choosing a new value in TDBLookupComboBox contents will scroll the dataset to the correct record) | + | * |
+ | Ensure the visible data in the TDBLookupComboBox is from the selected record in the dataset (i.e. choosing a new value in TDBLookupComboBox contents will scroll the dataset to the correct record) | ||
* Set the initial displayed value in the TDBLookupComboBox and the selected record in the datset to previously remembered values. | * Set the initial displayed value in the TDBLookupComboBox and the selected record in the datset to previously remembered values. | ||
Revision as of 10:43, 22 July 2017
│
English (en) │
français (fr) │
TDBLookupComboBox est un TComboBox lié aux données (doublement). Il peut être trouvé sur l'onglet Data Controls de la palette de composants.
Le contrôle TDBLookupCombobox obtient une liste de valeur depuis sa ListSource qui représente la donnée d'une autre table (p.ex. des produits). Alors, il
- affiche les valeurs dans la ListField (p.ex. le champ "NomDeProduit")
- se rappelle des valeurs dans la propriété KeyField (p.ex. un champ "ID")
Le ComboBox stocke le résultat (i.e. la valeur KeyField) dans la propriété DataField, p.ex. le champ "ProduitID" dans la propriété DataSource (p.ex. une table "Commandes").
La différence avec le TDBComboBox est que le TDBComboBox est un "contrôle individuellement lié aux données" (en quelque sorte).
Utilisation sans liaison
Vous pouvez obtenir la combobox pour rechercher des valeurs d'une table sans enregistrer les résultats dans une autre en laissant les propriétés DataSource et KeyField 'vides.
L'extrait de code exemple suivant :
- permettra à un TDBLookupComboBox d'être configuré en mode non lié (i.e. sans aucun changement porté dans la base de données)
Ensure the visible data in the TDBLookupComboBox is from the selected record in the dataset (i.e. choosing a new value in TDBLookupComboBox contents will scroll the dataset to the correct record)
- Set the initial displayed value in the TDBLookupComboBox and the selected record in the datset to previously remembered values.
The following sample code snipper will NOT:
- Automatically update the TDBLookupComboBox value if the dataset is scrolled (you need to add your own handler to Dataset.OnScroll to achieve this)
interface
type
TForm1 = class(TForm)
cboLookup: TDBLookupComboBox;
dsSource: TDatasource;
dsetSource: TDataset;
...
protected
FDisplayField: string;
FKeyField: string;
FPreviousKeyValue: string; // May be any datatype - see below
...
end;
...
implementation
...
// Remember previous value
FPreviousKeyValue:= load_from_settings_in_your_preferred_way_;
...
// Configure DBLookupCombo
dsSource.Dataset := dsetSource;
cboLookup.ListSource := dsSource;
cboLookup.ScrollListDataset := True; // This ensures that changing the ComboBox will automatically scroll
// the dataset
cboLookup.Style := csDropDownList;
cboLookup.KeyField := FKeyField;
cboLookup.ListField := FDisplayField; // This is the field that will appear in the contents of the ComboBox
dsetSource.Open; // The ComboBox should now be populated, however intially no text is displayed
// So right now there is no relationship between the selected record in the dataset
// and the contents of the ComboBox
// cboLookup.KeyValue reads and writes a Variant, so you may actually define
// FPreviousKeyValue as any data type you feel is appropriate.
// Remember a previous value
if FPreviousKeyValue <> '' then
begin
dsetSource.Locate(FKeyField, FPreviousKeyValue, []); // Note: This will fail to correctly set the ComboBox,
// But will set the Datatset to the correct record.
// Now the dataset is on the correct record, lets ensure the DBLookupCombo
// is showing the same value as the selected record
If (cboLookup.KeyValue = Null) And (dsetSource.RecordCount > 0) Then
cboLookup.KeyValue := dsetSource.FieldByName(FKeyField).AsVariant;
end;
...
// Save current value for next time
FPreviousKeyValue:= cboLookup.KeyValue;
write_to_settings_in_your_preferred_way_(FPreviousValue);
Bugs
At least for TDBLookupComboBox, there is a bug with FPC 2.6.0 (used in Lazarus 1.0, 1.0.2 etc) that requires the listfield to be present in the datasource as well.
Workaround: you can bypass this by declaring a calculated field with the same name as the listfield in the datasource's dataset that does nothing.
Contrôle alternatif
Les contrôles Rx (dans le paquet RxNew) ont le RxDBLookupCombobox qui dispose de fonctionnalités supplémentaires ; il permet p.ex. d'afficher de multiples champs/colonnes à côté l'un de l'autre (un peu comme les ComboBox MS Access) :
RxDBLookupCombo.LookupDisplay = 'field1;field2'; //takes a semicolon-delimited list of fields
Il a en plus une propriété DisplayAllFields (soit afficher tous les champs).
Voir aussi