代码
private
void
SetSortQuick(ReportClientDocument objRPT) {
if
(
!
String.IsNullOrEmpty(
this
.SortField)) { cCreateReport objCreateReport
=
new
cCreateReport();
string
[] arrySortField
=
this
.SortField.Split(
new
char
[] {
'
,
'
});
string
[] arrySortDirection
=
this
.SortDirection.Split(
new
char
[] {
'
,
'
});
for
(
int
i
=
0
; i
<
arrySortField.Length; i
++
) {
string
[] arryTemp
=
arrySortField[i].Split(
new
char
[] {
'
|
'
});
string
strFieldName
=
arryTemp[
0
];
string
strFieldType
=
arryTemp[
1
].ToLower();
string
strFormula
=
""
, strFormulaName
=
""
;
switch
(strFieldType) {
case
"
datetime
"
: strFormula
=
"
date({
"
+
arrySortField[i]
+
"
})
"
;
break
;
case
"
numeric
"
:
case
"
int
"
:
case
"
money
"
:
case
"
float
"
: strFormula
=
"
ToNumber({
"
+
arrySortField[i]
+
"
})
"
;
break
;
default
: strFormula
=
"
{
"
+
arrySortField[i]
+
"
}
"
;
break
; } strFormulaName
=
"
Order
"
+
i.ToString(); objRPT.DataDefController.FormulaFieldController.AddByName(strFormulaName, strFormula, CrFormulaSyntaxEnum.crFormulaSyntaxCrystal); ISCRField objS
=
objRPT.DataDefController.FindFieldByFormulaForm(
"
{@
"
+
strFormulaName
+
"
}
"
);
if
(objRPT.DataDefController.SortController.CanSortOn(objS)) { SortClass objSort
=
new
SortClass(); objSort.SortField
=
objS;
int
countOfSort
=
objRPT.DataDefinition.Sorts.Count;
if
(arrySortDirection[i]
==
"
1
"
) { objSort.Direction
=
CrSortDirectionEnum.crSortDirectionDescendingOrder; }
else
{ objSort.Direction
=
CrSortDirectionEnum.crSortDirectionAscendingOrder; } objRPT.DataDefController.SortController.Add(countOfSort, objSort); } } } }
必须使用这种方式才可以动态添加公式为排序字段
objRPT.DataDefController.FormulaFieldController.AddByName(strFormulaName, strFormula, CrFormulaSyntaxEnum.crFormulaSyntaxCrystal); ISCRField objS = objRPT.DataDefController.FindFieldByFormulaForm("{@"+strFormulaName+"}");
我试图使用如下方式动态添加公式字段,但是用CanSortOn检测总是不能作为sort字段。
代码
public
void
AddFormula(ReportClientDocument oReportClientDocument,
string
szName,
string
szFormula, CrFieldValueTypeEnum crFieldValueType ) { FormulaField oFormulaField; oFormulaField
=
new
FormulaField(); oFormulaField.Name
=
szName; oFormulaField.Text
=
szFormula; oFormulaField.Syntax
=
CrFormulaSyntaxEnum.crFormulaSyntaxCrystal; oFormulaField.Type
=
crFieldValueType; oReportClientDocument.DataDefController.FormulaFieldController.Add(oFormulaField); }
代码
public
FormulaField FindFormulaField(ReportClientDocument oReportClientDocument,
string
szFormula) { FormulaField oFormulaField; Fields oFields;
int
iIndex; oFormulaField
=
new
FormulaFieldClass(); oFields
=
oReportClientDocument.DataDefinition.FormulaFields; iIndex
=
oFields.Find(szFormula, CrFieldDisplayNameTypeEnum.crFieldDisplayNameName, CeLocale.ceLocaleUserDefault); oFormulaField
=
((FormulaField)oFields[iIndex]);
return
oFormulaField; }
后来我发现找到的公式字段的IsRecurring属性是false.只有这个属性为true时才可以作为sort的字段。
上面的方法就可以。不知道这是为什么。
转载于:https://www.cnblogs.com/lfzwenzhu/archive/2010/10/12/1848739.html
相关资源:《C#经典编程220例》.(明日科技).【带书签】-共3部分