Einzelnen Beitrag anzeigen

Benutzerbild von Bummi
Bummi

Registriert seit: 15. Jun 2010
Ort: Augsburg Bayern Süddeutschland
3.470 Beiträge
 
Delphi XE3 Enterprise
 
#2

AW: dynamische Spaltenanzeige vom CxGrid

  Alt 8. Okt 2012, 07:57
Da wird mehr drin sein als Du brauchst, aber das was Du suchst sollte auch dabei sein.

Delphi-Quellcode:
CLASS Procedure TTemplate.FillGridFromDataset(cxGrid: TcxGrid;ADS: TADODataSet;WithFooters:Boolean=true;SummaryKind:TcxSummaryKind=skSum;ReadonlyStyle: TcxStyle = nil;HiddenList:TStringList=nil;WidthList:TStringList=nil;displayformatList:TStringList=nil);
var
  i:Integer;
  g:TcxGridDBColumn;
  gsi,si:TcxGridDBTableSummaryItem;
  View:TcxGridDBTableView;
  cur:TCursor;

  Procedure SetVisible;
    begin
      if Assigned(Hiddenlist) then
        if Hiddenlist.IndexOfName(ADS.Fields[i].FieldName)>-1 then ADS.Fields[i].Visible := False;
    end;
  Procedure SetWidth;
    var
      w:Integer;
    begin
      if Assigned(WidthList) then
        if WidthList.IndexOfName(ADS.Fields[i].FieldName)>-1 then
          if TryStrToInt( WidthList.Values[ADS.Fields[i].FieldName],w) then ADS.Fields[i].DisplayWidth := w;
    end;
  Procedure SetDisplayFormat;
  begin
    if Assigned(displayformatList) then
      if displayformatList.IndexOfName(ADS.Fields[i].FieldName)>-1 then
        try
          if Ads.Fields[i] is TNumericField then
            TNumericField(ADS.Fields[i]).DisplayFormat:=displayformatList.Values[ADS.Fields[i].FieldName];
          if Ads.Fields[i] is TDateTimeField then
            TDateTimeField(ADS.Fields[i]).DisplayFormat:=displayformatList.Values[ADS.Fields[i].FieldName];
        except
          // Todo except
        end;
  end;

  Procedure SetFixedCols;
    var
      w:Integer;
      bv:TcxGridDBBandedTableView;
    begin
   if cxGrid.ActiveLevel.GridView is TcxGridDBBandedTableView then
      begin

      bv := TcxGridDBBandedTableView(cxGrid.ActiveLevel.GridView);
      if Assigned(WidthList) then
        if WidthList.IndexOfName('FIXEDCOLS')>-1 then
          if TryStrToInt( WidthList.Values['FIXEDCOLS'],w) then FixColumns(bv,w);
      end;
    end;

begin
  cur := Screen.Cursor;
  Screen.Cursor := crHourGlass;

  try
  ADS.DisableControls;
  View := TcxGridDBtableView(cxGrid.ActiveLevel.GridView);
  View.OptionsBehavior.FocusCellOnTab := true;
  for I := View.DataController.Summary.FooterSummaryItems.Count - 1 downto 0 do View.DataController.Summary.FooterSummaryItems[i].Free;
  for I := View.ColumnCount - 1 downto 0 do View.Columns[i].Free;
  if WithFooters then View.OptionsView.GroupFooters := gfVisibleWhenExpanded else View.OptionsView.GroupFooters := gfInvisible;
  for I := 0 to ADS.FieldCount - 1 do
        begin
          //if (ADS.Fields[i].DataType in [ftBCD,ftFMTBCD]) and (ADS.Fields[i].Tag<>-777) then TFloatField(ADS.Fields[i]).currency := true;
          if (ADS.Fields[i].DataType in [ftBCD,ftFMTBCD]) and (ADS.Fields[i].Tag<>-777) then TFloatField(ADS.Fields[i]).DisplayFormat := '#,##0.00';
          SetVisible;
          SetWidth;
          SetDisplayFormat;
          if ADS.Fields[i].Visible then
          begin
          g := View.CreateColumn;
          g.DataBinding.FieldName := ADS.Fields[i].FieldName;
          if Assigned(ReadonlyStyle) and ((ADS.Fields[i].Datatype = ftAutoInc) or ADS.Fields[i].ReadOnly) then
                begin
                g.Styles.Content := ReadonlyStyle;
                g.Options.Focusing := false;
                g.Editing := false;
                end;
          View.OptionsView.Footer := WithFooters;
          if WithFooters then
              begin
// if ADS.Fields[i].Datatype in [ ftSmallint, ftInteger, ftWord, ftBoolean, ftFloat, ftCurrency, ftBCD, ftfmtbcd, ftDate, ftTime, ftDateTime, ftAutoInc, ftLargeint] then
                  if (ADS.Fields[i].Datatype in [ ftCurrency, ftBCD, ftfmtbcd]) or ( ADS.Fields[i].Tag=333) then
                      begin
                        if not ((ADS.Fields[i].Datatype in [ ftDate, ftTime, ftDateTime] ) and (SummaryKind in [skSum])) then
                          begin
                          si:=TcxGridDBTableSummaryItem.Create(View.DataController.Summary.FooterSummaryItems);
                          si.Column := g;
                          si.FieldName := g.DataBinding.FieldName;
                          si.Kind := SummaryKind;
                                begin
                                gsi:=TcxGridDBTableSummaryItem.Create(View.DataController.Summary.DefaultGroupSummaryItems);
                                gsi.Column := g;
                                gsi.FieldName := g.DataBinding.FieldName;
                                gsi.Kind := SummaryKind;
                                gsi.Position := spFooter;
                                end;
                          end;
                      end;
              end;
          end;
        end;
  finally

  ADS.EnableControls;
// View.ApplyBestFit();
  SetOneGridWidth(cxGrid);
  SetFixedCols;
  Screen.Cursor := cur;
  end;
end;
Thomas Wassermann H₂♂
Das Problem steckt meistens zwischen den Ohren
DRY DRY KISS
H₂ (wenn bei meinen Snipplets nichts anderes angegeben ist Lizenz: WTFPL)
  Mit Zitat antworten Zitat