大家好!
  在cxgrid 中,如何在同一列显示数字和百分号。
  请见下图:
  
  如上图所示。数量栏位可以显示数字及百分号!谢谢!

解决方案 »

  1.   

    procedure TForm1.cxGrid1DBTableView1Column2GetDisplayText(
      Sender: TcxCustomGridTableItem; ARecord: TcxCustomGridRecord;
      var AText: String);//“数量”列取显示文本
    begin
      if pos('率', cxGrid1DBTableView1.DataController.Values[ARecord.Index,0])>0 then//如果当前记录中的“项目”列中有“率”字
        AText:=Sender.EditValue+'%';//在当前列的值后面附加“%”符号
    end;
      

  2.   

    gzzai
       您好!
       因为列是在程序中动态生成的。如何在动态的列中加入此行代码呢? 
      

  3.   

    gzzai
       您好!
       列是动态生成的。代码如下:
    var
      sSQL: string;
      i,j: Integer;
      cl: TcxGridDBBandEdColumn;
      cb:TcxGridBand;
      wk:string;
      vDate:string;
    begin
      inherited;
      self.CompanyName.Caption := PrintCompanyName;
      self.Title.Caption := PrintTitle;
      sSQL:= PrintSql;
      try
        Qryprint.DisableControls;
        if not Assigned(frmShowMsg) then
           frmShowMsg := TfrmShowMsg.Create(Application);
        frmShowMsg.Show;
        frmShowMsg.Update;
        Qryprint.Connection := DllAdoCn;
        dlladocn.CommandTimeout :=500;
        Qryprint.Close;
        Qryprint.SQL.Clear;
        Qryprint.SQL.Add(sSQL);
        Qryprint.Open;
        Screen.Cursor:=crHourGlass;
        cxGrid1DBBandedTableView1.BeginUpdate;
        wk:='';
        j:=0;
        try
          for i:=3 to cxGrid1DBBandedTableView1.DataController.DataSet.FieldCount-1 do
          begin
            vDate:=cxGrid1DBBandedTableView1.DataController.DataSet.Fields[i].FieldName;
            if vDate <>'合计' then
            begin
              vDate :=COPY(vDate,1,4)+'-'+COPY(vDate,5,2)+'-'+COPY(vDate,7,2);
              if wk<> ('W'+IntToStr(WeekOf(StrToDate(vDate)))) then
              begin
                cb:=cxGrid1DBBandedTableView1.Bands.Add;
                cb.Caption :=('W'+IntToStr(WeekOf(StrToDate(vDate))));
                cl:=cxGrid1DBBandedTableView1.CreateColumn;
                cl.DataBinding.FieldName:=cxGrid1DBBandedTableView1.DataController.DataSet.Fields[i].FieldName;
                cl.Caption :=copy(vDate,6,5);
                cl.HeaderAlignmentHorz :=taCenter;
                cl.HeaderAlignmentVert :=vaCenter;
                cl.Position.BandIndex :=j+1;
                wk :='W'+IntToStr(WeekOf(StrToDate(vDate)));
                j:=j+1;
              end
              else
              begin
                cl:=cxGrid1DBBandedTableView1.CreateColumn;
                cl.DataBinding.FieldName:=cxGrid1DBBandedTableView1.DataController.DataSet.Fields[i].FieldName;
                cl.Caption :=copy(vDate,6,5);
                cl.HeaderAlignmentHorz :=taCenter;
                cl.HeaderAlignmentVert :=vaCenter;
                cl.Position.BandIndex :=j;
              end;
            end
            else
            begin
              cb:=cxGrid1DBBandedTableView1.Bands.Add;
              cb.Caption :='';
              cl:=cxGrid1DBBandedTableView1.CreateColumn;
              cl.DataBinding.FieldName:=cxGrid1DBBandedTableView1.DataController.DataSet.Fields[i].FieldName;
              cl.HeaderAlignmentHorz :=taCenter;
              cl.HeaderAlignmentVert :=vaCenter;
              cl.Position.BandIndex :=j+1;
              j:=j+1;
            end;
          end;
        finally
          cxGrid1DBBandedTableView1.EndUpdate;
        end;
      finally
        Qryprint.EnableControls;
        frmShowMsg.Close;
        frmShowMsg.Free;
        Screen.Cursor:=crDefault;
      end;
    end;如何在新生成的列中添加GetDisplayText事件呢!
    谢谢!
      

  4.   

    用SQL在后台生成你要的数据,必要时用CAST强制转换即可。