怎样给dbgrid1的每一行的第一列画一个panel,把 当前的行号写在panel上。 

解决方案 »

  1.   

    如果只是 当前的行号写在panel上。可以不用画panel,添加一个计算字段就可以,如果要画的话,在dbgrid的DrawDataCell事件处理。
      

  2.   

    我是想在DBGrid1DrawColumnCell处理,但不知怎么写.
      

  3.   

    可以把当前的行号付给  panel1.caption,  但panel1.不知道怎样画进dbgrid1的每一行,的第一列。
      

  4.   

    看你命苦,帮你找了找,终于找到简单的方法    TStringGrid(DBGrid1).FixedCols:=2;
         TStringGrid(DBGrid1).Options:=[goFixedVertLine,goFixedHorzLine,goVertLine,goHorzLine];这次总没问题了吧。
      

  5.   

    你用这种方法,FixedCols:=2;这样会在第一列标题头有些问题,会变为白色
      

  6.   

    我用的是dbgrideh,不是dbgrid,所以不能用这种方法,如果是这样上午就解决了。 不过还是谢谢你
      

  7.   

    TStringGrid(DBGrid1).Options:=[goFixedVertLine,goFixedHorzLine,goVertLine,goHorzLine];看你一天都在忙这个,很是过意不去。
      

  8.   

    这两条语句如果用在dbgrid上完全可以解决问题了。 但是我用在dbgridehehlib2.4这个东东,所以 执行这两句话时 那一列变为白色了。
      

  9.   

    我不用第三方控件,没办法帮你了,不过我想你可以打开dbgrided和StringGrid两个源文件比较一下,把dbgrideh的相关代码修改。不过增强的DBGrid往往都有多列标题等功能,重绘部分的代码可能更复杂。
      

  10.   

    我想应该并没有那么复杂吧。就是画一个panel表列上面, 把列号给panel1.caption.我刚才看了一段代码,但我不会修改。我找找
      

  11.   

    话题0915735的标题是: 试着做了个控件,帮我看看 
    分类:控件-开发 Dk108 (2002-2-8 14:15:00)  
    刚做了个控件,想在DrawGrid的每一行的第一列加上一个CheckBox,但是编译以后每次添加
    到一个Form上,在DrawGrid上都有闪烁,而且运行也看不到CheckBox,那位高手能帮我看看是什么
    原因!
    unit XzlDrawGrid;interfaceuses
        windows,SysUtils,Classes,Grids,Graphics,Messages,Controls,StdCtrls,Forms;
    type
        TXzlDrawGrid=class(TDrawGrid)
           private
                  fcolor:TColor;
                  procedure setcolor(const value:TColor);
           public
                 CheckBox:Array[1..100]of TCheckBox;
                 constructor Create(AOwner:TComponent);override;
                 destructor  Destroy;override;
                 procedure DrawCell(ACol,ARow:LongInt;ARect:TRect;AState:TGridDrawState);override;
           published
                 property mycolor:TColor read fcolor write setcolor;    end;procedure Register;
    implementationprocedure Register;
    begin
         RegisterComponents('Samples',[TXzlDrawGrid]);
    end;constructor TXzlDrawGrid.Create(AOwner:TComponent);
    var
       i:integer;
    begin
         inherited Create(AOwner);
         FColor := clBtnFace;
         for i:=1 to col do
            CheckBox[i]:=TCheckBox.create(Self);
    end;destructor TXzlDrawGrid.Destroy;
    begin
         inherited;
    end;procedure TXzlDrawGrid.setcolor(const value:TColor);
    begin
         Fcolor:=value;
         refresh;
    end;procedure TXzlDrawGrid.DrawCell(ACol,ARow:LongInt;ARect:TRect;AState:TGridDrawState);
    begin
         if (ACol=1)and(ARow<>0) then
         begin
              CheckBox[ACol].Parent:=Self;
              CheckBox[ACol].Left:=ARect.Left;
              CheckBox[ACol].Top:=ARect.Top;
              CheckBox[ACol].Height:=ARect.Bottom-ARect.Top;
              CheckBox[ACol].Show;
         end;
         inherited;
    end;
    end. 
     
    taozhiyu (2002-2-8 14:18:00)  
    你create的东西不用free的么? 
     
    tseug (2002-2-8 14:18:00)  
    constructor TXzlDrawGrid.Create(AOwner:TComponent);
    var
       i:integer;
    begin
         inherited Create(AOwner);
         FColor := clBtnFace;
         for i:=1 to col do
            begin
            CheckBox[i]:=TCheckBox.create(Self);
            CheckBox[i].Parent := Self
            end;
    end 
     
    minikiller (2002-2-8 14:36:00)  
    up 
     
    Dk108 (2002-2-8 14:41:00)  
    to taozhiyu:
     但是我要用到checkbox,怎么能free了呢?to tseug:
        Checkbox[i].parent我在DrawCell中附值了呀:) 
     
    tseug (2002-2-8 14:46:00)  
    我觉得在那里是有问题的。[:(],你试试吧,我这样做过没甚么问题。 
     
    Dk108 (2002-2-8 14:49:00)  
    to tseug:
        我试了一下,把checkbox[i].parent加在create里面后,控件出错,可能是self还没有
    创建的原因吧,你试过吗?可以吗?有没有源码? 
     
    tseug (2002-2-8 15:55:00)  
    正在看,晚上告诉你。 
     
    LargeWang (2002-2-8 17:46:00)  
    想给你改也不行啊,我放假了 
     
    balaschen (2002-2-8 19:35:00)  
    你的控件存在以下问题:
    1、CheckBox没有Free;
    2、你在DrawCell中实际上只用到一个CheckBox,而你一下子定义了100个CheckBox,
    简直是资源杀手;
    3、你的处理思路根本性就是错误的,由于你都是用到同一个CheckBox来显示,所以当你的
    显示下一行的时候CheckBox就相应的往下移动,这就是为什么在你要显示CheckBox的那一列
    一直闪烁的原因。不改变你处理方式的话只能是每行用一个CheckBox,这将极大的浪费资源
    你把if (ACol=1)and(ARow<>0) then改成if (ACol=1)and(ARow=1) then或者
    改成:
    if (ACol=1)and(ARow<>0) then
         begin
              CheckBox[ARow].Parent:=Self;
              CheckBox[ARow].Left:=ARect.Left;
              CheckBox[ARow].Top:=ARect.Top;
              CheckBox[ARow].Height:=ARect.Bottom-ARect.Top;
              CheckBox[ARow].Show;
         end
         else
         inherited;再看看,但这不是解决之道,正确的方法是在DrawCell方法中自己画CheckBox,可以画
    CheckBox样子的图片或利用DrawFrameControl函数来显示。 
     
    tseug (2002-2-8 19:52:00)  
    大概改了一下,你看看吧
    unit abc;interfaceuses
        windows,SysUtils,Classes,Grids,Graphics,Messages,Controls,StdCtrls,Forms;
    type
        TXzlDrawGrid=class(TDrawGrid)
           private
                  fcolor:TColor;
                  procedure setcolor(const value:TColor);
           public
                 CheckBox:Array[1..100]of TCheckBox;
                 constructor Create(AOwner:TComponent);override;
                 destructor  Destroy;override;
           published
                 property mycolor:TColor read fcolor write setcolor;    end;procedure Register;
    implementationprocedure Register;
    begin
         RegisterComponents('Samples',[TXzlDrawGrid]);
    end;constructor TXzlDrawGrid.Create(AOwner:TComponent);
    var
       I    : Integer;
       Rect : TRect;
    begin
         inherited Create(AOwner);
         Parent := TWinControl(AOWner);
         FColor := clBtnFace;     for I := 1 to RowCount do
            begin
            Rect   := CellRect(1, I);
            CheckBox[I]:=TCheckBox.Create(Self);
            with CheckBox[I] do
              begin
              Name   := Format('CheckBox%d', [I]);
              Parent := Self;
              Left   := Rect.Left;
              Top    := Rect.Top;
              Width  := Rect.Right - Rect.Left;
              Height := Rect.Bottom - Rect.Top;
              end;
            end;
    end;destructor TXzlDrawGrid.Destroy;
    var
     i : Integer;
    begin
      for i := 1 to col do
        begin
        CheckBox[i].Free;
        end;
      inherited;
    end;procedure TXzlDrawGrid.setcolor(const value:TColor);
    begin
         Fcolor:=value;
         refresh;
    end;end. 
     
    zj_94yy (2002-2-8 19:55:00)  
    我觉得你可以自己画一个CheckBox,没有必要用CheckBox控件 
     
    liu_liu (2002-2-9 12:50:00)  
    同意balaschen,可以在DBGrid的OnDBGrid1DrawDataCell事件中获得位置来显示CheckBox如果是做控件的话,应该在DBGrid1DrawDataCell事件中进行控制 
     
    Dk108 (2002-2-13 17:29:00)  
    to tseug:我用你的方法试了,还是一样的闪烁不停to zh96yy:自己画一个CheckBox?怎么做?能详细点吗?谢谢 :) 
     我想把这一段代码改为花panel, 怎么做
      

  12.   

    那如果你有10000条记录不是要画10000个Panel?真是好笑!
      

  13.   

    又一种在DBGrid中完全可以解决的方式,在DBGrideh中应该也没有问题。procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
      DataCol: Integer; Column: TColumn; State: TGridDrawState);
    var P: TPanel;
    begin
         if Column.Title.caption='1' then
         begin
              P:=TPanel.Create(DBGrid1);
              P.Left:=Rect.Left;
              P.Top:=Rect.Top;
              P.Width:=Rect.Right-Rect.Left;
              P.Height:=Rect.Bottom-Rect.Top;
         end;
    end;
      

  14.   

    先要算出Panel的个数,并定义一个Panel数组在DBGrid1DrawDataCell中加上Panel[i]:=TPanel.Create(DBGrid1);
    Panel[i].Left:=Rect.Left;
    Panel[i].Top:=Rect.Top;
      

  15.   

    奇怪,老兄,你用的是TDBGridH 控件,怎么扯到 TDBGrid和TDrawGrid控件?
      

  16.   

    我想TDBGridH 是从dbgrid继承下来的。
      

  17.   

    这里是第一行第一列显示Check的简易实现。如果要换成Panel。实现基本类似
    unit wfpDrawGrid;interfaceuses
      Windows, Messages, SysUtils, Classes, Controls, Grids, QGraphics;type
      TwfpDrawGrid = class(TDrawGrid)
      private
        FbCheck: Boolean;  protected
        procedure DrawCell(ACol, ARow: Longint; ARect: TRect;
          AState: TGridDrawState); override;
        procedure DrawCheck(ARect: TRect);
        procedure MouseUp(Button: TMouseButton; Shift: TShiftState;
          X, Y: Integer); override;
      public
        constructor Create(AOwner: TComponent); override;
        { Public declarations }
      published
        { Published declarations }
      end;procedure Register;implementationprocedure Register;
    begin
      RegisterComponents('Samples', [TwfpDrawGrid]);
    end;{ TwfpDrawGrid }constructor TwfpDrawGrid.Create(AOwner: TComponent);
    begin
      inherited;
      FbCheck := True;
    end;procedure TwfpDrawGrid.DrawCell(ACol, ARow: Integer; ARect: TRect;
      AState: TGridDrawState);
    begin
      if (ARow = 1) and (ACol = 1) then begin
        DrawCheck(ARect);
      end
      else
        inherited;
    end;procedure TwfpDrawGrid.DrawCheck(ARect: TRect);
    var
      iCheckWidth: Integer;
      iWidth, iHeight: Integer;
      iTop, iLeft: Integer;
      rctCheck: TRect;
      clSave: TColor;
    begin
      iWidth := ARect.Right - ARect.Left;
      iHeight := ARect.Bottom - ARect.Top;
      if iWidth > iHeight then begin
        iCheckWidth := iHeight;
      end
      else
        iCheckWidth := iWidth;
      if iCheckWidth > 16 then begin
        iCheckWidth := 16;
      end;
      iTop := ARect.Top + (iHeight - iCheckWidth) shr 1;
      iLeft := ARect.Left + (iWidth - iCheckWidth) shr 1;
      rctCheck := Rect(iLeft, iTop, iLeft + iCheckWidth, iTop + iCheckWidth);  clSave := Canvas.Pen.Color;
      Canvas.Pen.Color := clBlack;
      //画边框
      Canvas.Rectangle(rctCheck);
      if FbCheck then begin
        //画勾
        Canvas.Pen.Width := 2;
        Canvas.MoveTo(iLeft + 2, iTop + (iCheckWidth shr 1) - 3);
        Canvas.LineTo(iLeft + (iCheckWidth shr 1) - 3, rctCheck.Bottom - 2);
        Canvas.LineTo(rctCheck.Right - 2, iTop + 2);
        Canvas.Pen.Width := 1;
      end;
      Canvas.Pen.Color := clSave;
    end;procedure TwfpDrawGrid.MouseUp(Button: TMouseButton; Shift: TShiftState; X,
      Y: Integer);
    var
      pCellHit: TGridCoord;
    begin
      //计算点击的格子位置
      pCellHit := MouseCoord(X, Y);
      if (pCellHit.X = 1) and (pCellHit.Y = 1) then begin
        FbCheck := not FbCheck;
        InvalidateCell(pCellHit.Y, pCellHit.X);
      end;
      inherited;
    end;end.
      

  18.   

    真是越说越乱了,你不是要显示行号吗? 怎么又说到TCheckBox了?TDBGridH从TDBGrid继承而来,说不定她在里边已经做了一些手脚,就算你对TDBGrid画成功了,对TDBGridH也未必成功。
      

  19.   

    唉,没做过数据管理方面的程序,说起话来真是底气不足啊!!!!!手头一个数据库文件都没有,具体怎么使用TDBGrid也不知道。
      

  20.   

    是呀,越搞越复杂。 我是要把行号显示在panel上, 这个panel1是画在每一行的第一列上的。
      

  21.   

    那还不一样啊。就是把重写一下DrawCell。判断一下是否每一行的第一列,如果是,就把行号TextOut出来就行了。前天我还刚好对TdxDBGrid(Express Developer公司的Grid组件,比TDBGridEh好用的不知多少倍。)做修改,就是在指示栏中,显示当前行号。跟你的需求差不多。前面我那些代码是显示CheckBox的,刚刚为你写的。通过测试没有问题的。
      

  22.   

    那当然要判断一下罗。你完全可以判断DataSet.State,只有为dsBrowse时,画行号,其他情况继承父类实现。
      

  23.   

    这次是调试过了,没有问题!procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
      DataCol: Integer; Column: TColumn; State: TGridDrawState);
    var R: TRect;
    begin
         if Column.Index=0 then
         begin
              R:=Rect;
              R.Left:=R.Left;
              R.Top:=R.Top;
              R.Right:=R.Right;
              R.Bottom:=R.Bottom;
              DBGrid1.Canvas.Brush.Color:=clBtnFace;
              DBGrid1.Canvas.FillRect(R);
              DBGrid1.Canvas.Pen.Color:=clWhite;
              DBGrid1.Canvas.MoveTo(R.Left,R.Bottom-1);
              DBGrid1.Canvas.LineTo(R.Left,R.Top);
              DBGrid1.Canvas.LineTo(R.Right-1,R.Top);
              DBGrid1.Canvas.Pen.Color:=clGray;
              DBGrid1.Canvas.MoveTo(R.Left,R.Bottom-1);
              DBGrid1.Canvas.LineTo(R.Right-1,R.Bottom-1);
              DBGrid1.Canvas.LineTo(R.Right-1,R.Top);
              DBGrid1.Canvas.Pen.Color:=clBlack;
              DBGrid1.Canvas.MoveTo(R.Left-1,R.Bottom);
              DBGrid1.Canvas.LineTo(R.Left-1,R.Top-1);
              DBGrid1.Canvas.LineTo(R.Right,R.Top-1);
              DBGrid1.Canvas.LineTo(R.Right,R.Bottom);
              DBGrid1.Canvas.LineTo(R.Left-1,R.Bottom);
              DBGrid1.Canvas.TextOut(R.Left+10,R.Top+2,DBGrid1.DataSource.DataSet.FieldByName(Column.FieldName).asString);
         end;
    end;
      

  24.   

    不好意思呀, 让大家久等了。我在这里谢谢大家。 刚刚才上来呀, 我的adsl一直上不去。
      

  25.   

    是的。 固定的数据列。  用johnsonrao(johnson)提供的方法,这样不会影响速度吧。 我只要求固定1。.1000行。 用户最多只能输入1000行。
      

  26.   

    我把它的option属性dgedit属设为假了,这样他就不能编辑。 但所有的都不能编辑。 如果只让最前列不能编辑固定可以做到吗?