现在用DevExpress的cxGrid用的特别多,但是里面的属性设置非常多,所以我考虑,自己写一个继承自此控件的控件。把一些经常要设置的通用属性、事件封装在新组件里。
procedure TU_cxGrid.SetStyle();
begin
    LookAndFeel.Kind := lfFlat;
    LookAndFeel.NativeStyle := false;
    Width:=320;{*************************************************************************
问题就在此:不管用什么方式,得到的view=nil,这样FGridView=nil,所以后面的就没办法做了。
但是我在Create里,有Inherited Create(AOwner);这样GridView应该是创建好了的呀,但是为什么结果为nil呢?    FGridView:=TcxGridDBTableView(inherited FocusedView);
    FGridView:=TcxGridDBTableView(inherited ActiveView);
    FGridView:=TcxGridDBTableView(FocusedView);
    FGridView:=TcxGridDBTableView(ActiveView);
    FGridView:=TcxGridDBTableView(Views[0]);
**************************************************************************}    FGridView.OptionsBehavior.AlwaysShowEditor := true;
    FGridView.OptionsBehavior.FocusCellOnCycle := true;
    FGridView.OptionsBehavior.FocusCellOnTab := true;
    FGridView.OptionsBehavior.FocusFirstCellOnNewRecord := true;
    FGridView.OptionsBehavior.GoToNextCellOnEnter := true;    FGridView.OptionsView.ExpandButtonsForEmptyDetails := false;
    FGridView.OptionsView.GroupByBox:=false;
    FGridView.OptionsView.Footer:=true;
    FGridView.OptionsView.Indicator := true;//由于上面的问题,我不得不该变了想法:在基类中直接修改源代码,修改一些属性的default值。OK.还好这种方式能够对上面的这些属性达到预期的效果,但是先面的Styles属性设置,和自动创建Column就不知道怎么写了?    FStyleEven.Color:=clWindow;
    FStyleOdd.Color:=clInactiveBorder;
    FGridView.Styles.ContentEven := FStyleEven;
    FGridView.Styles.ContentOdd := FStyleOdd;        
    FGridDBColumn:=FGridView.CreateColumn;
    FGridDBColumn.Name := 'colSN';
    FGridDBColumn.Caption := 'SN';
    FGridDBColumn.HeaderAlignmentHorz:=taCenter;
    FGridDBColumn.FooterAlignmentHorz:=taCenter;
    FGridDBColumn.Options.Filtering:=false;
    FGridDBColumn.Options.Editing:=false;
    FGridDBColumn.Options.Focusing:=false;
    FGridDBColumn.Options.Sorting:=false;
    FGridDBColumn.MinWidth:=30;
    FGridDBColumn.Width:=40;
    FGridDBColumn.PropertiesClass:=TcxTextEditProperties;
    FGridDBColumn.Properties.Alignment.Horz:=taCenter;
    FGridDBColumn.Properties.ReadOnly:=true;
    FGridDBColumn.OnGetDisplayText:=MyGetDisplayText;end;
constructor TU_cxGrid.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
    FStyleEven:=TcxStyle.Create(AOwner);
    FStyleOdd:=TcxStyle.Create(AOwner);    SetStyle();
end;
这个控件我弄了好几天,虽然弄的我头都大了,但是我一定要把它搞定,这样在后面的工作中就减少了时间,提高了效率。多谢高手们一起进来研究一哈,在线等各位~!best wishes~~~

解决方案 »

  1.   

    我看了看源代码:
    constructor TcxCustomGrid.Create(AOwner: TComponent);
    begin
      inherited;
      BorderStyle := cxcbsDefault;
      ControlStyle := ControlStyle + [csDisplayDragImage];
      FDragOpening := True;
      FDragOpeningWaitTime := cxGridDefaultDragOpeningWaitTime;
      FNotifications := TList.Create;
      FViews := TList.Create;
      FChanges := TList.Create;
      FChangesStack := TList.Create;
      FTabStop := True;
      Levels := GetLevelsClass.Create(nil);
      CreateHandlers;
      Width := 250;
      Height := 200;
    end;里边只建立了一个FViews,并没有缺省建立一个View,FViews列表为空。你可以这样修改一下:
    constructor TU_cxGrid.Create(AOwner: TComponent);
    var
      aGridLevel: TcxGridLevel;
    begin
      inherited Create(AOwner);
      FGridView := CreateView(TcxGridDBTableView);
      aGridLevel := Levels.add;
      aGridLevel.GridView := FGridView;
      FStyleEven:=TcxStyle.Create(AOwner);
      FStyleOdd:=TcxStyle.Create(AOwner);  SetStyle();
    end;还有,我觉得你这样做似乎意义不大啊。该套件中还提供了TcxPropertiesStore、TcxLookAndFeelController、TcxStyleRepository等控件,它们的作用就是为了方便、快捷地定制大量控件的外观、风格等特性的。实在不行,还有控件的拷贝粘贴大法嘛。
      

  2.   

    不过 FGridView := CreateView(TcxGridDBTableView); 好象参数类型不对,不能编译。不管怎么样,非常感谢你的解答。