如题,找了网上资料,基本没什么结果。想了变通方式(即:不使goRowSelect为true,选择一个cell时,该行背景变色),但是问了问题还没找到比较好的方式(http://topic.csdn.net/u/20090319/14/0bf7ea36-d0c0-40c9-8da3-1428906a0333.html)。 所以我查看了Delphi7的相关源码,改了一些东西,基本 达到了我的要求。不过由于本人水品有限,不知这样改后会不会有较大的危害,或有更简单的方式达到目标。请大家帮忙看看,谢谢关注,Thanks。改了5处:
一:注释掉 一点东西:function TCustomGrid.CanEditShow: Boolean;
begin
  Result := {([goRowSelect, goEditing] * Options = [goEditing]) and   }
    FEditorMode and not (csDesigning in ComponentState) and HandleAllocated and
    ((goAlwaysShowEditor in Options) or IsActiveControl);
end;二:TCustomGrid  protected 下添加MyPoint:TPoint;
Procedure MyPro(ACol,ARow: Integer);procedure TCustomGrid.MyPro(ACol,ARow: Integer);
var ss:TGridRect;
    tt:TRect;
begin
  MyPoint.X:=ACol;
  MyPoint.Y:=ARow;  ss.Left:=1;
  ss.Top:=Row;
  ss.Right:=ColCount-1;
  ss.Bottom:=Row;
  GridRectToScreenRect(ss, tt, True);
  Windows.InvalidateRect(Handle, @tt, False);
end;三:TCustomGrid.ShowEditor中添加点东西,添加后为:procedure TCustomGrid.ShowEditor;
begin
  if (goEditing in Options)and(goRowSelect in Options) then
  begin
    FAnchor.X:=MyPoint.X;
    FAnchor.Y:=MyPoint.y;
    FCurrent.X:=MyPoint.X;
    FCurrent.y:=MyPoint.y;
  end;  FEditorMode := True;
  UpdateEdit;
end;四:procedure TCustomGrid.MouseDown(Button: TMouseButton; Shift: TShiftState;X, Y: Integer); 中改一句话//        if (CellHit.X = FCurrent.X) and (CellHit.Y = FCurrent.Y) then            //这句注释掉
        if (CellHit.X = MyPoint.X) and (CellHit.Y = MyPoint.Y) then                //改为这句
五:function TCustomDrawGrid.SelectCell(ACol, ARow: Longint): Boolean;中添加一句,改后为:function TCustomDrawGrid.SelectCell(ACol, ARow: Longint): Boolean;
begin
  MyPro(ACol, ARow);  //加的这句
  Result := True;
  if Assigned(FOnSelectCell) then FOnSelectCell(Self, ACol, ARow, Result);
end;

解决方案 »

  1.   

    这句话太空洞了吧,消息响应谁都知道,VCL结构也不是那么容易搞清楚的吧。
    我想要的就是 这一点点的技巧 。
      

  2.   

    可以输入字符和数字,汉字还没研究出来有什么简单方法procedure TForm1.FormShow(Sender: TObject);
    begin
      self.vg_col:=1;
      self.vg_row:=1;
    end;procedure TForm1.StringGrid1SelectCell(Sender: TObject; ACol,
      ARow: Integer; var CanSelect: Boolean);
    begin
      self.vg_col:=ACol;
      self.vg_row:=ARow;
    end;procedure TForm1.StringGrid1KeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    var
      s:widestring;
    begin
      s:=self.StringGrid1.Cells[self.vg_col,self.vg_row];
      if (Key>48) and (Key<=128) then
        self.StringGrid1.Cells[self.vg_col,self.vg_row]:=s+char(key);
      if Key=8 then
      begin
        self.StringGrid1.Cells[self.vg_col,self.vg_row]:=Copy(s,1,Length(s)-1);
      end;
      if Key=32 then
      begin
        self.StringGrid1.Cells[self.vg_col,self.vg_row]:=s+' ';
      end;
    end;
      

  3.   

    这就相当于所有可selectcell的cell一直都要处于编辑状态了,那就没有 编辑状态 和 非编辑状态 之分了。这个StringGrid1用起来得比较小心。
      

  4.   

    在选择的单元格中绑定一个edit吧
    Edit1.SetBounds,绑定到单元格区域
      

  5.   

    StringGrid1SelectCell事件中,控制哪行选择不选择,控制能列能编辑,那列不能编辑.关与不能编辑列和能编辑列的底色,也可以在这里控制.StringGrid1SetEditText该事件,对编辑中的内容进行控制,如有数字和填入价格,这里就能算出金额.StringGrid1KeyPress(TObject *Sender, char &Key) 该事件,对某格的内容进行控件,如只能数字,或只能字母等.StringGrid1DrawCell该事件中,是自画,如某格中,画一个按钮,或编辑框或下拉框等.
      

  6.   

    现在用BC,呵呵,不改写了.DELPHI跟BC差不多.
    =======================================================================
    void __fastcall Tfm_ly_bill::sgSelectCell(TObject *Sender, int ACol,
          int ARow, bool &CanSelect)
    {
     if(sg->RowCount==ARow+1) {   CanSelect=false;  return;  } //最后一行不整行显示
     if((Tag==0||Tag==1)&&(ACol==5||ACol==8))
         sg->Options=sg->Options<<goEditing<<goAlwaysShowEditor<<goTabs;
      else sg->Options=sg->Options>>goAlwaysShowEditor>>goEditing>>goTabs;
     //=========================
     sg->Refresh();
     TRect irect;
     for(int i=1;i<sg->ColCount-1;i++)
     {
       irect=sg->CellRect(i,ARow);
       if((Tag==0||Tag==1)&&(i==5||i==8)) sg->Canvas->Brush->Color=0x0012266B;
         else     sg->Canvas->Brush->Color=0x00C56A31;  // 0x00804000
       sg->Canvas->FillRect(irect);
       sg->Canvas->Font->Color=clWhite;
       sg->Canvas->TextRect(irect,irect.Left+1,irect.Top+1,sg->Cells[i][ARow]);
     }  //end for        
    }
      

  7.   

    噢.TAG=0 是新开.TAG=1 是编辑   只有5,8列是可写的.一定要刷新一下,
    然后下面就是画上不同的色彩,以示区别.你的条件可以不必这么长的.