我在stringgrid中每一行加入了一个按钮,点击按钮后show出另一个窗体,然后双击这个stringgrid中的一行值后赋给另一个主stringgrid?在那一行的按扭上点的就赋给那一行?

解决方案 »

  1.   

    onselectcell事件中写你想要的代码
      

  2.   

    //参考如下代码
    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, ComCtrls, StdCtrls, Menus, Grids;type
      TForm1 = class(TForm)
        StringGrid1: TStringGrid;
        procedure FormCreate(Sender: TObject);
      private
        { Private declarations }
        procedure ButtonClick(Sender: TObject);
      public
        { Public declarations }
      end;var
      Form1: TForm1;implementationuses Unit2;{$R *.dfm}var
      vButtons: array of TButton;procedure TForm1.ButtonClick(Sender: TObject);
    begin
      if Form2.ShowModal <> mrOK then Exit;
      StringGrid1.Rows[StringGrid1.Row] :=
        Form2.StringGrid1.Rows[Form2.StringGrid1.Row];
    end;procedure TForm1.FormCreate(Sender: TObject);
    var
      I: Integer;
      vRect: TRect;
    begin
      SetLength(vButtons, StringGrid1.VisibleRowCount);
      for I := Low(vButtons) to High(vButtons) do
      begin
        vRect := StringGrid1.CellRect(StringGrid1.ColCount - 1, I);
        vButtons[I] := TButton.Create(StringGrid1);
        vButtons[I].Parent := StringGrid1.Parent;
        OffsetRect(vRect, StringGrid1.Left + 2, StringGrid1.Top + 27);
        vButtons[I].BoundsRect := vRect;
        vButtons[I].Tag := I;
        vButtons[I].OnClick := ButtonClick;
      end;
    end;end./////////////////----------------------------unit Unit2;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, Grids;type
      TForm2 = class(TForm)
        StringGrid1: TStringGrid;
        procedure StringGrid1DblClick(Sender: TObject);
        procedure FormCreate(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;var
      Form2: TForm2;implementation{$R *.dfm}procedure TForm2.StringGrid1DblClick(Sender: TObject);
    begin
      ModalResult := mrOK;
    end;procedure TForm2.FormCreate(Sender: TObject);
    var
      I, J: Integer;
    begin
      for I := 0 to StringGrid1.ColCount - 1 do
        for J := 0 to StringGrid1.RowCount - 1 do
          StringGrid1.Cells[I, J] := Format('%d,%d', [I, J]);
    end;end.
      

  3.   

    procedure TForm1.ButtonClick(Sender: TObject);
    begin
      StringGrid1.Row := StringGrid1.TopRow + TButton(Sender).Tag; //如果行数多加上这一条
      if Form2.ShowModal <> mrOK then Exit;
      StringGrid1.Rows[StringGrid1.Row] :=
        Form2.StringGrid1.Rows[Form2.StringGrid1.Row];
    end;procedure TForm1.FormCreate(Sender: TObject);
    var
      I: Integer;
      vRect: TRect;
    begin
      StringGrid1.RowCount := 15;
      SetLength(vButtons, StringGrid1.Height div StringGrid1.DefaultRowHeight - 1); //这里修改
      for I := Low(vButtons) to High(vButtons) do
      begin
        vRect := StringGrid1.CellRect(StringGrid1.ColCount - 1, I);
        vButtons[I] := TButton.Create(StringGrid1);
        vButtons[I].Parent := StringGrid1.Parent;
        OffsetRect(vRect, StringGrid1.Left + 2, StringGrid1.Top + 27);
        vButtons[I].BoundsRect := vRect;
        vButtons[I].Tag := I;
        vButtons[I].OnClick := ButtonClick;
      end;
    end;
      

  4.   

    谢谢 zswang兄弟!一会给你结帐!