2016-11-14 94 views
1

这是推动我坚果.... 我取回我的数据库的名称来填充cxComboBox1:缺少的表名称属性

procedure TForm1.FormShow(Sender: TObject); 
var 
    I: Integer; 
    DBList: TStringDynArray; 
begin 
    DBList := TDirectory.GetFiles(ExtractFilePath(ParamStr(0)), '*.abs', TSearchOption.soAllDirectories); 
    for I := 0 to Length(DBList) - 1 do 
    begin 
    cxCombobox1.Properties.Items.Add(DBList[I]); 
    end; 
end; 

这工作我的数据库OK.The列表中显示的cxCombobox1 。

在第二个cxCombobox中,我填充属于数据库 的表名称,显示在cxCombobox1中。

procedure TForm1.cxComboBox1PropertiesChange(Sender: TObject); 
var 
    TABLES: TStringList; 
    i: integer; 
begin 
    if ABSTable1.Active = True then 
    ABSTable1.Close; 
    cxComboBox2.properties.Items.Clear; 
    TABLES := TStringList.Create; 
    ABSDatabase1.DatabaseFileName:=cxCombobox1.Text; 
    try 
    ABSDatabase1.Open; 
    ABSDatabase1.GetTablesList(TABLES); 
    for i:= 0 to TABLES.Count-1 do 
     cxComboBox2.properties.Items.Add(TABLES[i]); 
    finally 
    TABLES.free; 
    end; 
end; 

这基本上工作正常。选择cxComboBox1中的数据库将使用相关表格填充cxComboBox2 。

所以一般的想法是在cxComboBox2中选择时打开表格。 而我所做的:

procedure TForm1.ABSTable1BeforeOpen(DataSet: TDataSet); 
begin 
    ABSTable1.DatabaseName:= ABSDatabase1.DatabaseName; 
    ABSTable1.TableName := cxComboBox2.Text; 
end; 

而且在combobox2更改事件我所做的:

procedure TForm1.cxComboBox2PropertiesChange(Sender: TObject); 
begin 
    cxGrid1DBTableView1.ClearItems; 
    ABSTable1.Open; 
    cxGrid1DBTableView1.DataController.CreateAllItems; 
end; 

该工程确定。但是只有当我打开combobox2中显示的选定数据库(combobox1) 中的表格时。 说我打开了表格,然后去选择combobox1中的另一个数据库 我收到错误“Missing ABSTable1.Tablename”!

我在这里错过了什么?我的桌子名称在哪里丢失? 如果我更换change事件combobox2一个按钮:

procedure TForm1.cxButton2Click(Sender: TObject); 
begin 
    if ABSTable1.Active = True then 
    ABSTable1.Close; 
    cxGrid1DBTableView1.ClearItems; 
    ABSTable1.Open; 
    cxGrid1DBTableView1.DataController.CreateAllItems; 
end; 

然后一切工程....

+1

'cxComboBox2.properties.Items.Clear;'触发组合框2的更改事件。在combobox2的更改事件中,仅当在combobox2中选择一个表时才打开数据库。 – NineBerry

回答

1

这是因为你在cxComboBox1PropertiesChange调用Items.Clear()这引起了一个变化事件,在返回的结果中致电cxComboBox2PropertiesChange

在更新之前应使用Items.BeginUpdate(),并在完成更新时使用Items.EndUpdate()以避免在每个更新的步骤/项目上引发事件。

+0

所以你建议我应该使用cxCombobox1。更改事件:cxComboBox2.Properties.Items.BeginUpdate(); cxComboBox2.properties.Items.Clear; ???我在哪里使用EndUpdate? – user3351050

+0

总是用'BeginUpdate()'成对调用'EndUpdate()'。因此,使用'try'' finally'构造完全适合这里,而在Begin和EndUpate()在finally块中被调用之前调用'BeginUpdate()'。 – ViRuSTriNiTy