在c#中动态生成水晶报表时,动态添加sort

代码
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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值