Код:
procedure TFrmMain.DBGrdEhWeightsApplyFilter(Sender: TObject);
function LastPos(SubStr, S: string): Integer;
var
Found, Len, Pos: integer;
begin
Pos:= Length(S);
Len:= Length(SubStr);
Found:= 0;
while (Pos > 0) and (Found = 0) do
begin
if Copy(S, Pos, Len) = SubStr then
Found:= Pos;
Dec(Pos);
end;
LastPos:= Found;
end;
var
s, FText: string;
i: integer;
begin
s:= '';
with (Sender as TDBGridEh) do
for i:= 0 to Columns.Count - 1 do
if Columns[i].STFilter.ExpressionStr <> '' then
begin
if i = (Col-1) then
FText:= FilterEdit.Text // текущая колонка
else
FText:= Columns[i].STFilter.ExpressionStr; // все остальные колонки
// Если фильтр выбран мышкой (Ehlib сам подставляет)
if ((FText[1] = '=') and // одно значение
(FText[2] = '''') and
(FText[Length(FText)] = '''')) or
((FText[1] = 'i') and // несколько
(FText[2] = 'n') and
(FText[4] = '(') and
(FText[Length(FText)] = ')')) then
s:= s + Columns[i].FieldName + ' ' + FText + ' and '
else // во всех остальных случаях (при вводе руками)
begin
if Columns[i].Field.DataType in [ftWideString, ftString] then
s:= s + Columns[i].FieldName + ' like ''%' + FText + '%'' and '
else
if Columns[i].Field.DataType in [ftSmallint, ftInteger, ftWord, ftBoolean,
ftFloat, ftCurrency, ftBCD, ftDate, ftTime, ftDateTime] then
s:= s + Columns[i].FieldName + ' = ' + FText + ' and ';
end;
end;
if LastPos(' and ', s) = Length(s) - 4 then
Delete(s, Length(s) - 4, Length(s) - 4); // удаляем ' and ', если он есть в конце
MTWeights.Filter:= s;
actCancelFilterWeights.Enabled:= MTWeights.Filter <> '';
//DBGridEh1.DefaultApplyFilter();
end;
procedure TCustomDBGridEh.InplaceEditKeyDown(Control: TWinControl;
var Key: Word; Shift: TShiftState);
begin
if (Key = VK_TAB) then
begin
FilterEdit.Text:= ReplaceStr(FilterEdit.Text, FilterEdit.SelText, '');
ApplyFilter; // применяем фильтр
end;
inherited;
end;
procedure TCustomDBGridEh.InplaceEditKeyUp(Control: TWinControl; var Key: Word;
Shift: TShiftState);
begin
if {Какое-то условие в настройках на автофильтр} True and
(Key <> VK_TAB) then
begin
// убираем выделение
FilterEdit.Text:= ReplaceStr(FilterEdit.Text, FilterEdit.SelText, '');
// применяем фильтр
ApplyFilter;
// позиция курсора в фильтре
FilterEdit.SelStart:= Length(FilterEdit.Text);
end;
inherited;
end;