2009-09-09 57 views
1

DevExpressグリッドのフィルタからfiltertype演算子を取得するために使用するコードニッペードは次のとおりです。 OperatorKindToStrは、演算子をフィルタから文字列として抽出し、xmlファイルに格納するために使用します。 StrToOperatorKindは、xmlの文字列をフィルターに戻すために変換するために使用されます。DevExpress TcxFilterOperatorKindを文字列に変換したり、文字列から変換したりできますか?

const 
    CUSTFILTER_FILTERITEM  = 'FilterItem'; 

function OperatorKindToStr(const aOperatorKind: TcxFilterOperatorKind): string; 
begin 
    Result := 'foEqual'; 
    case aOperatorKind of 
    foEqual:  Result := 'foEqual'; 
    foNotEqual:  Result := 'foNotEqual'; 
    foLess:   Result := 'foLess'; 
    foLessEqual: Result := 'foLessEqual'; 

    // Plus a boring list of other constants 
end; 

function StrToOperatorKind(const aOpKindStr: string): TcxFilterOperatorKind; 
begin 
    Result := foEqual; 
    if aOpKindStr  = 'foNotEqual' then 
    Result := foNotEqual 
    else if aOpKindStr = 'foLess' then 
    Result := foLess 
    else if aOpKindStr = 'foLessEqual' then 
    Result := foLessEqual 
    else if aOpKindStr = 'foGreater' then 
    Result := foGreater 
    else if aOpKindStr = 'foGreaterEqual' then 
    Result := foGreaterEqual 

    // Plus a boring list of other if-else 
end; 

procedure UseStrToOperatorKind(const aFilterItem: IXmlDomElement); 
begin 
    if aFilterItem.nodeName = CUSTFILTER_FILTERITEM then 
    begin        // It is an FilterItem 
    vStr := VarToStr(aFilterItem.getAttribute(CUSTFILTER_COLPROP)); // Get the columnname 
    vOperatorKind := StrToOperatorKind(aFilterItem.getAttribute(CUSTFILTER_ITEMOPERATOR)); 
end; 

procedure UseOperatorKindToStr(const aFilterItem: TcxCustomFilterCriteriaItem); 
var 
    vStr: String; 
begin 
    if Supports(TcxFilterCriteriaItem(aFilterItem).ItemLink, TcxGridColumn, GridCol) then 
    vStr := OperatorKindToStr(TcxFilterCriteriaItem(aFilterItem).OperatorKind); 
end; 

どうやら私はStrToOperatorKindとOperatorKindToStrは少し賢くなりたいです。 VCL TypeInfoでGetEnumPropメソッドを試しましたが、機能しません。 したがって、aFilterItem変数からTcxFilterOperatorKindプロパティを文字列に抽出し、TcxFilterOperatorKindに戻すにはどうすればよいですか?

答えて

1

GetEnumNameGetEnumValueデュエットを使用してください。

そして、あなたの機能がはるかに簡単になるはずです。

function OperatorKindToStr(const aOperatorKind: TcxFilterOperatorKind): string; 
begin 
    Result := GetEnumName(TypeInfo(TcxFilterOperatorKind), Ord(aOperatorKind)); 
end; 

function StrToOperatorKind(const aOpKindStr: string): TcxFilterOperatorKind; 
begin 
    Result := TcxFilterOperatorKind(GetEnumValue(TypeInfo(TcxFilterOperatorKind), aOpKindStr)); 
end; 
+0

うん!それは正しかった。アドバイスをいただきありがとうございます。 –

1

GetEnumPropは、実行しようとしている機能に間違っているため機能しませんでした。あなたは近くにいる。 TypInfoユニットにもあるGetEnumNameとGetEnumValueを試してください。メイソンが指摘したように

関連する問題