如何使用数据库中的记录填充组合框

How to populate combobox with records in database

我正在尝试用我的数据库中的学校名称填充我的组合框。我已经使用以下代码对每个组合框的过程进行了硬编码:

procedure TfrmSportsDay.PopulateCmbOpponentASE;
var
  sLine: String;
begin
  cmbOpponentASE.Items.Clear;
  with DM_Sport do
  begin
    tblSchools.First;
    while not tblSchools.eof do
    begin
      sLine := tblSchools['SchoolName'];
      cmbOpponentASE.Items.Add(sLine);
      tblSchools.Next;
    end;
  end;
end;

这由我手动输入名称来填充特定的组合框,有没有一种方法可以在过程中输入组合框的名称,因此我有 1 个过程来填充组合框,而不必编写一个每个?

例如

Private
Procedure PopulateCmb(sComboBoxName : String);

Procedure PopulateCmb(sComboBoxName : String);
var
  sLine: String;
begin
  {sComboBoxName}.Items.Clear;
  with DM_Sport do
  begin
    tblSchools.First;
    while not tblSchools.eof do
    begin
      sLine := tblSchools['SchoolName'];
      {sComboBoxName}.Items.Add(sLine);
      tblSchools.Next;
    end;
  end;
end;

不是传递带有组合框名称的参数,而是传递作为参数的组合框变量(在您显示的代码中是 cmbOpponentASE)。

procedure PopulateCmb(Combo : TComboBox);
var
  sLine: String;
begin
  Combo.Items.Clear;
  with DM_Sport do
  begin
    tblSchools.First;
    while not tblSchools.eof do
    begin
      sLine := tblSchools['SchoolName'];
      Combo.Items.Add(sLine);
      tblSchools.Next;
    end;
  end;
end;

并这样称呼它:

PopulateCmb(cmbOpponentASE);

使用kbmMW SmartBinding,就这么简单:

Binding.Bind(DM_Sport.tblSchools,'SchoolName',cmbOpponentASE,'Items');

如果您希望数据集当前记录反映您在组合框中选择的项目,则还要添加:

Binding.Bind(DM_Sport.tblSchools,'@',cmbOpponentASE,'@',[mwboTwoWay]);

kbmMW 是一个完全支持 Delphi 和所有平台的工具箱。 在 http://components4developers.blog 阅读更多内容 搜索 SmartBind,您会发现 6 篇博文展示了它的易用性。