delphi ComboBox data binding

Delphi does not bind directly to the database as isolated as .Net data to the control above, for example, is a province of the query table, TcomboBox the item is displayed as the name of the provinces, the provinces get the corresponding id After selecting a province. delphi method is not so easy, consulted a large cattle-level Great God, learned a very easy way.

The first step in creating a class
of the type
THospitalClass = class (TObject)
Private
the Name: stirng; // Name
Id: Integer; // the above mentioned id
End;

The second step after creating the objects in a database query out data added to the ComboBox control

var
the Obj: THospitalClass;
{release object created, delphi created after the note must have deliverance}
DestoryHospital (cmbHospital);

Obj := THospitalClass.Caeate;
obj.name :=Query.FieldBYName(‘name’).AsString;
obj.Id :=Query.FieldBYName(‘name’).AsInteger;
cmdHospital.Items.AddOBject(obj.id,Obj);

The third step is to obtain id corresponding to the selected hospitals
var
Hospital: THospitalClass;

hospital :=THospital(cmbhospital.Items.Object[cmbhospital.ItemIndex]);

hosopital.id is the id we need to operate

procedure DestoryHospital(AcomboBox: TcomboBox)
var
i :Integer;
begin
for int i:=0 in AcomboBox.Items.Count-1 do
if Assigned(AcomboBox.items.Object[i])
AcomboBox.Items.Object[i].Destory;
AcomboBox.Clear;

End;
----------------
Disclaimer: This article is CSDN blogger "RunNick 'original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source and link this statement.
Original link: https: //blog.csdn.net/CSDNmyACCP_S2/article/details/52629644

Guess you like

Origin www.cnblogs.com/jijm123/p/12624047.html