怎样删除stringgrid的当前行?怎样得到stringgrid的当前行?怎样让stringgrid失去焦点时不选择其中一行?

解决方案 »

  1.   

    用LISTVIEW吧。
    你以上的三个问题都可以解决。用STRINGGRID要麻烦点。
      

  2.   

    给你代码参考:
    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
      Grids, Menus, StdCtrls;type
      TForm1 = class(TForm)
        StringGrid1: TStringGrid;
        PopupMenu1: TPopupMenu;
        N1: TMenuItem;
        N2: TMenuItem;
        Button1: TButton;
        Edit1: TEdit;
        procedure StringGrid1MouseDown(Sender: TObject; Button: TMouseButton;
          Shift: TShiftState; X, Y: Integer);
        procedure StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
          Rect: TRect; State: TGridDrawState);
        procedure FormCreate(Sender: TObject);
        procedure N2Click(Sender: TObject);
        procedure N1Click(Sender: TObject);
        procedure Button1Click(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;var
      Form1: TForm1;
      currentx,currenty,current_D,selected_row,selected_col:integer;
    implementation{$R *.DFM}
      FUNCTION XCenter (rect:  TRect; canvas:  TCanvas; s:  STRING):  INTEGER;
      BEGIN
        RESULT := ((rect.Left + rect.Right) - canvas.TextWidth(s)) DIV 2
      END {XCenter};  
      {Top of text is its origin, so adjust by half-height of text to center}
      FUNCTION YCenter (rect:  TRect; canvas:  TCanvas; s:  STRING):  INTEGER;
      BEGIN
        RESULT := ((rect.Top + rect.Bottom) - canvas.TextHeight(s)) DIV 2
      END {YCenter};procedure TForm1.StringGrid1MouseDown(Sender: TObject;
      Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
    begin
        currentx:=x;
        currenty:=y;
        StringGrid1.Repaint;
    end;procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
      Rect: TRect; State: TGridDrawState);
      VAR
        s:  STRING;
        re:Trect;
        procedure setzsdchecked;
        begin
            IF   ARow < (Sender AS TStringGrid).FixedRows
            THEN Begin
            //(Sender AS TStringGrid).Canvas.Brush.Color := clBtnFace
                N1.Enabled:=False;
                N2.Enabled:=False;
            End
            ELSE BEGIN
             //(Sender AS TStringGrid).Canvas.Brush.Color := clTeal;
              If (Rect.Top<currenty) and (currenty<Rect.Bottom) Then
              Begin
                  (Sender AS TStringGrid).Canvas.Brush.Color := clTeal;
                   N1.Enabled:=False;
                   N2.Enabled:=True;
                   current_D:=2;
                   selected_row:=Arow;
              End
              Else
              Begin
                  If ACol=0 Then
                     (Sender AS TStringGrid).Canvas.Brush.Color := clBtnFace
                  Else
                  (Sender AS TStringGrid).Canvas.Brush.Color := clWhite;
              End;
            End;
        end;    procedure settxchecked;
        begin
            IF   acol < (Sender AS TStringGrid).FixedCols
            THEN Begin//(Sender AS TStringGrid).Canvas.Brush.Color := clBtnFace
                N1.Enabled:=False;
                N2.Enabled:=False;
            End
            ELSE BEGIN
              If (Rect.Left<currentx) and (currentx<Rect.Right) Then
              Begin
                  (Sender AS TStringGrid).Canvas.Brush.Color := clTeal;
                   N1.Enabled:=True;
                   N2.Enabled:=False;
                   current_D:=1;
                   selected_col:=Acol;
              End
              Else
              Begin
                  If ARow=0 Then
                     (Sender AS TStringGrid).Canvas.Brush.Color := clBtnFace
                  Else
                  (Sender AS TStringGrid).Canvas.Brush.Color := clWhite;
              End;
            END;
        end;
    begin
      s := (Sender AS TStringGrid).Cells[acol,arow];  (Sender AS TStringGrid).Canvas.Font.Color := clBlack;
      If (0<currentx) and (currentx<StringGrid1.ColWidths[0]) Then
      Begin
          setzsdchecked;
      End
      Else Begin
          If (0<currenty) and (currenty<StringGrid1.RowHeights[0]) Then
          Begin
              settxchecked;
          End
          Else Begin
              If current_D=2 Then
              Begin
                  setzsdchecked;
              End
              Else Begin
                  settxchecked;
              End;    
          End;
      End;  (Sender AS TStringGrid).Canvas.FillRect(Rect);
      (Sender AS TStringGrid).Canvas.TextRect(Rect,
                       XCenter(Rect, (Sender AS TStringGrid).Canvas, s),
                       YCenter(Rect, (Sender AS TStringGrid).Canvas, s), s)end;procedure TForm1.FormCreate(Sender: TObject);
    begin
        current_D:=0;
        StringGrid1.Cells[1,0]:='z';
        StringGrid1.Cells[2,0]:='h';
        StringGrid1.Cells[3,0]:='j';
        StringGrid1.Cells[4,0]:='k';
        StringGrid1.Cells[0,1]:='z';
        StringGrid1.Cells[0,2]:='h';
        StringGrid1.Cells[0,3]:='j';
        StringGrid1.Cells[0,4]:='k';
    end;procedure TForm1.N2Click(Sender: TObject);
      procedure deleterow(i:integer);
       var r,c,rcount :integer;
       begin
         with stringgrid1 do
         begin
           rcount:=rowcount;
           for r:=i to rcount-2 do
             for c:=0 to colcount-1 do
               cells[c,r]:=cells[c,r+1];
          dec(rcount);
          RowCount:=rcount;
        end
      end;
    begin
      deleterow(selected_row);
    end;procedure TForm1.N1Click(Sender: TObject);
      procedure deletecol(i:integer);
       var r,c,ccount :integer;
       begin
         with stringgrid1 do
         begin
           ccount:=colcount;
           for c:=i to ccount-2 do
             for r:=0 to rowcount-1 do
               cells[c,r]:=cells[c+1,r];
          dec(ccount);
          colcount:=ccount;
        end
      end;
    begin
      deletecol(selected_col);
    end;procedure TForm1.Button1Click(Sender: TObject);
      procedure Insertzsd(const zsddh:String;DestStrGrid:TStringGrid);
      var tempzsddh:String;
          id1,id2,id3:integer;
      Begin
          tempzsddh:=zsddh;
          For id1:=1 To DestStrGrid.RowCount-1 Do
          Begin
              If Pos('('+tempzsddh+')',DestStrGrid.Cells[0,id1])=1 Then
              Begin
                  currentx:=DestStrGrid.CellRect(0,id1).left+1;
                  currenty:=DestStrGrid.CellRect(0,id1).top+1;
                  DestStrGrid.Invalidate;
                  Exit;
              End;
          End;
          tempzsddh:=Copy(tempzsddh,1,Length(tempzsddh)-2);
          While Length(tempzsddh)>0 Do
          Begin
              For id1:=DestStrGrid.RowCount-1 DownTo 1 Do
              Begin
                  If Pos('('+tempzsddh,DestStrGrid.Cells[0,id1])=1 Then
                  Begin
                      with DestStrGrid do
                      begin
                        RowCount:=RowCount+1;
                        for id2:=RowCount-1 Downto id1+2  do
                          for id3:=0 to colcount-1 do
                            cells[id3,id2]:=cells[id3,id2-1];
                        For id2:=0 to colcount-1 do
                        Begin
                            cells[id2,id1+1]:='';
                        End;
                        cells[0,id1+1]:='('+zsddh+')';
                        currentx:=DestStrGrid.CellRect(0,id1+1).left+1;
                        currenty:=DestStrGrid.CellRect(0,id1+1).top+1;
                        DestStrGrid.Invalidate;
                        Exit;
                      End;
                  End;
              End;
              tempzsddh:=Copy(tempzsddh,1,Length(tempzsddh)-2);
          End;
          DestStrGrid.RowCount:=DestStrGrid.RowCount+1;
          For id2:=0 to DestStrGrid.colcount-1 do
          Begin
              DestStrGrid.cells[id2,DestStrGrid.RowCount-1]:='';
          End;
          DestStrGrid.cells[0,DestStrGrid.RowCount-1]:='('+zsddh+')';
          currentx:=DestStrGrid.CellRect(0,DestStrGrid.RowCount-1).left+1;
          currenty:=DestStrGrid.CellRect(0,DestStrGrid.RowCount-1).top+1;
      End; 
    begin
      Insertzsd(edit1.text,stringgrid1);
    end;end.
      

  3.   

    楼上好夸张...!我做过的:
    怎样删除stringgrid的当前行?
    procedure TForm1.btnDeleteClick(Sender: TObject); //删除按钮
    var
      i, j :integer;
    begin
      with StringGrid1 do
      if RowCount-1 > FixedRows then //如果不是第一可用行
      begin
        for i := Row to RowCount -2 do
          for j := Col to ColCount -2 do
            Cells[j, i] := Cells[j, i + 1];
        RowCount := RowCount -1;
      end else
      begin //如果是第一可用行则清空数据
        for j := Col to ColCount -2 do
          Cells[j, Row] := '';
      end;
    end;怎样得到stringgrid的当前行?
      ShowMessage(IntToStr(StringGrid1.Row)+','+IntToStr(StringGrid1.Col));
                           ---------------               ----------------
                               当前行                          当前列怎样让stringgrid失去焦点时不选择其中一行?
    设置StringGrid1的Options的goAlwaysShowEditor = True;goEditing = True;