Главная страница
Top.Mail.Ru    Яндекс.Метрика
Текущий архив: 2008.04.13;
Скачать: CL | DM;

Вниз

Помогите, пожалуйста, оптимизировать заполнение ComboBox-ов   Найти похожие ветки 

 
Федор   (2008-03-16 10:29) [0]

Помогите оптимизировать заполнение ComboBox-ов:
if ADOQuery1.Active then ADOQuery1.Close;
ADOQuery1.SQL.Text := "SELECT DISTINCT P1, P2, P3 FROM Base "+
 "ORDER BY 1,2,3";
 ADOQuery1.Open;
 while not ADOQuery1.EOF do
 begin
  if ComboBox1.Items.IndexOf(ADOQuery1.Fields[0].asString)=-1
  then begin
   if ADOQuery1.Fields[0].asString<>""
    then ComboBox1.Items.Add(ADOQuery1.Fields[0].asString);
  end;
  if ComboBox2.Items.IndexOf(ADOQuery1.Fields[1].asString)=-1
  then begin
   if ADOQuery1.Fields[1].asString<>""
    then ComboBox2.Items.Add(ADOQuery1.Fields[1].asString);
  end;
  if ComboBox3.Items.IndexOf(ADOQuery1.Fields[2].asString)=-1
  then begin
   if ADOQuery1.Fields[2].asString<>""
    then ComboBox3.Items.Add(ADOQuery1.Fields[2].asString);
  end;
  ADOQuery1.Next;
 end;

После нескольких попыток получилось вот это...
Может есть более практичный вариант?


 
Kolan ©   (2008-03-16 10:41) [1]

Во превых можно и нужно уменьшить кол-во кода и сделать его более понятным.

Тут пахнет Duplicate Cod"ом.

Интерфейс примерно такой:
procedure FillCombo(ADataSet: TDataSet; AComboBox: TComboBox);


 
Kolan ©   (2008-03-16 10:42) [2]

Еще используй ComboBox.Items.BeginUpdate и ComboBox.Items.EndUpdate.


 
Kolan ©   (2008-03-16 10:46) [3]

> Интерфейс примерно такой:
> procedure FillCombo(ADataSet: TDataSet; AComboBox: TComboBox)
> ;

procedure FillCombo(ADataSet: TDataSet; AComboBox: TComboBox; AFieldIndex: Integer);


 
Федор   (2008-03-16 10:49) [4]

хм...

procedure FillCombo(ADataSet: TDataSet; AComboBox: TComboBox; AFieldIndex: Integer);
if ADataSet.Active then ADataSet.Close;
ADataSet.SQL.Text := "SELECT DISTINCT P1, P2, P3 FROM Base "+
"ORDER BY 1,2,3";
ADataSet.Open;
while not ADataSet.EOF do
begin
 if AComboBox.Items.IndexOf(ADataSet.Fields[AFieldIndex].asString)=-1
 then begin
  if ADataSet.Fields[AFieldIndex].asString<>""
   then AComboBox.Items.Add(ADataSet.Fields[AFieldIndex].asString);
 end;
 ADataSet.Next;
end;

Ты что-то подобное имеешь ввиду?


 
Kolan ©   (2008-03-16 11:00) [5]

> Ты что-то подобное имеешь ввиду?

Зачем запрос запихал туда?

if ADataSet.Active then ADataSet.Close;
ADataSet.SQL.Text := "SELECT DISTINCT P1, P2, P3 FROM Base "+
"ORDER BY 1,2,3";
ADataSet.Open;

procedure FillCombo(ADataSet: TDataSet; AComboBox: TComboBox; AFieldIndex: Integer);
begin
 while not ADataSet.EOF do
 begin
   if AComboBox.Items.IndexOf(ADataSet.Fields[AFieldIndex].asString)=-1
   then
   begin
     if ADataSet.Fields[AFieldIndex].asString<>"" then
       AComboBox.Items.Add(ADataSet.Fields[AFieldIndex].asString);
   end;
   ADataSet.Next;
 end;
end;


Нехватает проверок и [2]


 
Kolan ©   (2008-03-16 11:01) [6]

А на счет DataAware контролов ты не думал?


 
Федор   (2008-03-16 11:21) [7]

Спасибо!
------------------------------------
и [2]                      - зачем?


 
Kolan ©   (2008-03-16 17:00) [8]

> &#151; зачем?

Шоб быстрее было.


 
ЦУП ©   (2008-03-16 17:21) [9]

procedure FillCombo(AComboBox: TComboBox; const Str: String);
begin
  if AComboBox.Items.IndexOf(Str)=-1 and Str<>"" then
     AComboBox.Items.Add(ADataSet.Fields[AFieldIndex].asString);
end;

while not ADOQuery1.EOF do
begin
 FillCombo(ComboBox1,ADOQuery1.Fields[0].asString);
 FillCombo(ComboBox2,ADOQuery1.Fields[0].asString);
 FillCombo(ComboBox3,ADOQuery1.Fields[0].asString);
 ADOQuery1.Next;
end;


 
ЦУП ©   (2008-03-16 17:22) [10]

Только вот тут опечатка:
if AComboBox.Items.IndexOf(Str)=-1 and Str<>"" then

Надо
if (AComboBox.Items.IndexOf(Str)=-1) and (Str<>"") then



Страницы: 1 вся ветка

Текущий архив: 2008.04.13;
Скачать: CL | DM;

Наверх




Память: 0.49 MB
Время: 0.017 c
15-1204380753
Unbekannt
2008-03-01 17:12
2008.04.13
Оборзевшие спамеры


2-1205758370
Sergey2
2008-03-17 15:52
2008.04.13
Убрать первоначальную форму.


2-1205583312
alex_kasycky
2008-03-15 15:15
2008.04.13
Залипающие кнопки


15-1204147672
Tirael
2008-02-28 00:27
2008.04.13
тем, кому не все равно


2-1205874786
deras
2008-03-19 00:13
2008.04.13
SQL запрос - выбрать все дублирующиеся записи.