DELPHI的DBGRID有两个难点

DELPHI的DBGRID有两个难点:
一,每行都是一个BUTTON,且CAPTION不同
二,每行不同行高,且由用户拖动而不同高的
这两点我用CXGRID全做到了
一,
procedure TForm1.cxGrid1DBTableView1Column1CustomDrawCell(
  Sender: TcxCustomGridTableView; ACanvas: TcxCanvas;
  AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
var
  www: tbutton;
  i, j: integer;
begin
www:=nil;
  try

  for i := AViewInfo.GridView.Site.ComponentCount - 1 downto 0  do
  begin
    if AViewInfo.GridView.Site.Components[i] is tbutton then
    begin
        tbutton(AViewInfo.GridView.Site.Components[i]).Tag := -1;
        if tbutton(AViewInfo.GridView.Site.Components[i]).Caption =
          TcxGridDataCellViewInfo(AViewInfo).DisplayValue then
        begin
          www := tbutton(AViewInfo.GridView.Site.Components[i]);
        end;
      end;

    end;
    if TcxGridDataCellViewInfo(AViewInfo).Item.Caption = 'NAME' then
    begin
      if www = nil then
      begin
        www := tbutton.Create(AViewInfo.GridView.Site);
        www.Visible:=false;
        www.Parent := AViewInfo.GridView.Site;
        www.Caption := TcxGridDataCellViewInfo(AViewInfo).DisplayValue; //inttostr(iii);
        www.Tag := 1;
      end;
      www.SetBounds(AViewInfo.ClientBounds.Left, AViewInfo.ClientBounds.Top,
        AViewInfo.ClientBounds.Right - AViewInfo.ClientBounds.Left, AViewInfo.ClientBounds.Bottom
          -
        AViewInfo.ClientBounds.Top);
        www.Visible:=true;
    end;
   except
   on e:exception do
   begin
   end
   end;
end;

procedure TForm1.cxGrid1DBTableView1TopRecordIndexChanged(Sender: TObject);
var
i,j:integer;
begin
try
  for i := cxgrid1.ActiveView.Site.ComponentCount - 1 downto 0  do
  begin
    if cxgrid1.ActiveView.Site.Components[i] is tbutton then
    begin
        tbutton(cxgrid1.ActiveView.Site.Components[i]).Tag := -1;
              for j := 0 to TcxGridDBtableView(cxgrid1.ActiveView).ViewInfo.RecordsViewInfo.VisibleCount do
               begin
          if tbutton(cxgrid1.ActiveView.Site.Components[i]).Caption =
            TcxGridDataRowViewInfo(TcxGridDBtableView(cxgrid1.ActiveView).ViewInfo.RecordsViewInfo.Items[j]).CellViewInfos[0].DisplayValue then
            tbutton(cxgrid1.ActiveView.Site.Components[i]).Tag := 1;
        end;
        if  tbutton(cxgrid1.ActiveView.Site.Components[i]).Tag = -1 then
        tbutton(cxgrid1.ActiveView.Site.Components[i]).Free;
    end;
    end;
    except
    end;
end;

procedure TForm1.cxGrid1DBTableView1ColumnSizeChanged(
  Sender: TcxGridTableView; AColumn: TcxGridColumn);
var
i,j:integer;
begin
try
  for i := cxgrid1.ActiveView.Site.ComponentCount - 1 downto 0  do
  begin
    if cxgrid1.ActiveView.Site.Components[i] is tbutton then
    begin
        tbutton(cxgrid1.ActiveView.Site.Components[i]).Tag := -1;
        end;
     if  tbutton(cxgrid1.ActiveView.Site.Components[i]).Tag = -1 then
        tbutton(cxgrid1.ActiveView.Site.Components[i]).Free;
    end;
    except
    end;
end;

二,
CXGRID中,只要GetCellHeight事件不为NIL,且optionsview中columnautowidth设为FALSE,即可以实现自动每行行高不同也。
如果要手工用户拖动而每行行高不同,可用下面代码
//
procedure TcxGridRowSizingObject.Init(const P: TPoint; AParams: TcxCustomGridHitTest);
var
www:tstringlist;
i,j:integer;
begin
  inherited;
  FRow := TcxCustomGridRow((AParams as TcxGridRowSizingEdgeHitTest).GridRecord);
if   AParams.ViewInfo  is  tcxgridindicatorrowitemviewinfo   then
begin
   www:= tstringlist(tcxgridindicatorrowitemviewinfo(    AParams.ViewInfo).GridView.Site.Tag);
if (www<>nil) then
begin
  j:=0;
  for i:=0 to www.Count-1 do
  begin
     if integer(www.Objects[i])=FRow.RecordIndex then
     begin
     www[i]:='-1';
     j:=1;
     break;
     end;
  end;
  if j=0 then
  www.AddObject('-1',pointer(FRow.RecordIndex));
  end;
end;
end;
//
procedure TForm1.Button1Click(Sender: TObject);
var
www:tstringlist;
begin
  adotable1.active := true;
  www:=tstringlist.Create;
  cxgrid1.ActiveView.Site.Tag:=integer(pointer(www));
end;
//
procedure TForm1.cxGrid1DBTableView1GetCellHeight(
  Sender: TcxCustomGridTableView; ARecord: TcxCustomGridRecord;
  AItem: TcxCustomGridTableItem;
  ACellViewInfo: TcxGridTableDataCellViewInfo; var AHeight: Integer);
var
www:tstringlist;
i,j:integer;
begin
www:=tstringlist(cxgrid1.activeview.site.tag);
if ((www<>nil) and (www.Count>0)) then
begin
  j:=0;
  for i:=0 to www.Count-1 do
  begin
     if integer(www.Objects[i])=arecord.RecordIndex then
     begin
     j:=strtoint(www[i]);
     break;
     end;
  end;
if j<>0 then
begin

if j=-1 then
www[i]:=inttostr(cxGrid1DBTableView1.OptionsView.DataRowHeight);

j:=strtoint(www[i]);
aheight:=j;
///
 cxGrid1DBTableView1ColumnSizeChanged(nil,nil);
 end;
end;

end;
///
procedure TForm1.cxGrid1DBTableView1ColumnSizeChanged(
  Sender: TcxGridTableView; AColumn: TcxGridColumn);
var
i,j:integer;
begin
try
  for i := cxgrid1.ActiveView.Site.ComponentCount - 1 downto 0  do
  begin
    if cxgrid1.ActiveView.Site.Components[i] is tbutton then
    begin
        tbutton(cxgrid1.ActiveView.Site.Components[i]).Tag := -1;
        end;
     if  tbutton(cxgrid1.ActiveView.Site.Components[i]).Tag = -1 then
        tbutton(cxgrid1.ActiveView.Site.Components[i]).Free;
    end;
   except
    end;
end;

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值