如何在Delphi 7中使用ComboBox过滤DBGrid中的数据?

如何使用ComboBox过滤DBGrid中的数据?

例如,如果ComboBox文本为“表1”,则DBGrid将显示表1。

如何在Delphi 7中使用ComboBox过滤DBGrid中的数据?

这是我尝试过的代码:

procedure TForm4.cbb2Change(Sender: TObject);
begin
  if cbb2.Text = 'Surat Masuk Edaran' then
    with DataModule1.zqry_srt_masuk_edaran do
    begin
      dbgrd1.DataSource.DataSet := 'ds_srt_masuk_edaran'
    end else

    if cbb2.Text = 'Surat Masuk Undangan' then
      with DataModule1.zqry_srt_masuk_undangan do
      begin
        dbgrd1.DataSource.DataSet := 'ds_srt_masuk_undangan'
      end else

      if cbb2.Text = 'Surat Masuk Lain-lain' then
        with DataModule1.zqry_srt_masuk_lain2 do
        begin
          dbgrd1.DataSource.DataSet := 'ds_srt_masuk_lain2'
        end;
end;
fangsufang 回答:如何在Delphi 7中使用ComboBox过滤DBGrid中的数据?

您不过滤DBGrid本身,而是过滤由其分配的DataSource提供的数据。

例如,将DBGrid的DataSource属性设置为TDataSource组件,并将数据源的DataSet属性设置为TTable组件。根据需要配置TTable来访问数据库。当用户在ComboBox中选择一个表时,请相应地设置TTable.TableName属性。

,

看起来Items包含表名。因此,您可以直接使用:

procedure TForm4.cbb2Change(Sender: TObject);
begin
  DataModule.Table.Close;
  DataModule.Table.TableName:= TComboBox(Sender).Text;
  DataModule.Table.Open;
end;

如果不是这种情况,则可以将Array中的String用作

Const
MyArray: array [0..CntTables] of string = ('Table1','Table2','Table3');

并将其用作

procedure TForm4.cbb2Change(Sender: TObject);
begin
  DataModule.Table.Close;
  DataModule.Table.TableName:= MyArray[TComboBox(Sender).ItemIndex];
  DataModule.Table.Open;
end;
本文链接:https://www.f2er.com/2981222.html

大家都在问