请问各位大侠: 如何做到修改stringgridr的cell内容后,按enter键确认,按esc键恢复原值??还有: 如何能做到Cell的显示格式根据数据库字段类型而变,如:字符类型的字段靠左对齐,整型数类型字段靠右对齐,浮点数类型靠右对齐并显示小数位急呀,最好有源码分析。。小第在此先谢了。。

解决方案 »

  1.   

    恢复原值必须保存旧值,至于对齐,我有段源代码给你,StringGrid的Drawcell事件写法;
    procedure TInputGrid.DrawCell(ACol, ARow: Integer; Rect: TRect; State: TGridDrawState);
    var
      iTextWidth,iTextLeft: Integer;
      tmpAlignment: TAlignment;
    begin
      inherited;
      if ARow<FixedRows then tmpAlignment:=taCenter
      else begin
        if ACol<Length(FColFormat) then
          tmpAlignment := FColFormat[ACol].Alignment
        else
          tmpAlignment := taLeftJustify;
      end;  iTextWidth := Canvas.TextWidth(Cells[ACol,ARow]);
      iTextLeft := 2;
      case tmpAlignment of
        taLeftJustify:  iTextLeft := 2;
        taRightJustify: iTextLeft := Rect.Right-Rect.Left-iTextWidth-2;
        taCenter:       iTextLeft := (Rect.Right-Rect.Left-iTextWidth) div 2;
      end;  Canvas.TextRect(Rect,Rect.Left+iTextLeft,Rect.Top+2,Cells[ACol,ARow]);
    end;上面这个是继承Drawcell方法,如果对stringgrid的OnDrawcell事件编程需要做一些改动,
    你自己改吧。
      

  2.   

    复:ppayun(~云彩) 
    大侠有恢复stringGrid 的cell内容的代码吗??
      

  3.   

    恢复stringGrid 的cell内容的代码
    放两个stringGrid,stringGrid1和stringGrid2
    stringGrid2.visibl:=false;var
        Form1: TForm1;
        i,j:integer;
        k:boolean;
        str:string;
    implementation{$R *.dfm}procedure TForm1.StringGrid1KeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    begin
        if Key=VK_RETURN then
        begin
            i:=stringgrid1.Col;
            j:=stringgrid1.row;
            str:=stringgrid2.Cells[i,j];
            stringgrid2.Cells[i,j]:=stringgrid1.Cells[i,j];
            k:=true;
        end;
        if (k=true) and (Key = VK_ESCAPE) then
        begin
            stringgrid1.Cells[i,j]:=str;
            stringgrid2.Cells[i,j]:=str;        
            k:=false;
        end;
    end;procedure TForm1.FormCreate(Sender: TObject);
    var i,j:integer;
    begin
        for i:=1 to stringgrid1.ColCount do
        begin
            for j:=1 to stringgrid1.RowCount do
            begin
                StringGrid1.Cells[i,j]:=inttostr(i)+inttostr(j);
                StringGrid2.Cells[i,j]:=inttostr(i)+inttostr(j);
            end;
        end;
        k:=false;
    end;procedure TForm1.StringGrid1Click(Sender: TObject);
    var i,j:integer;
    begin
        if k=false then
        begin
            for i:=1 to stringgrid1.ColCount do
            begin
                for j:=1 to stringgrid1.RowCount do
                begin
                    StringGrid1.Cells[i,j]:=StringGrid2.Cells[i,j];
                end;
            end;
        end;
    end;end.