procedure TxmzdForm.stglszdKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  if  (key=VK_DOWN)  and (stglszd.row = stglszd.RowCount-1) and (trim(stglszd.Cells[3,stglszd.row])<>'')  then
    AppendRow(stglszd.Row);  if  (key=VK_UP)  and (stglszd.row = stglszd.RowCount-1) and (trim(stglszd.Cells[3,stglszd.row])='')  then
    DeleteRow(stglszd.Row);
 
end;procedure TxmzdForm.DeleteRow(RowNum: integer);
var i: integer;
begin
  with stglszd do
  begin
    if RowNum<= fixedrows-1 then exit;
    if RowNum > rowcount-1 then exit;
    if RowNum <= rowcount-1 then begin
      if RowCount=FixedRows+1 then begin
        Rows[RowCount-1].Clear;
        exit;
      end;
      for i:=RowNum to RowCount-2 do  Rows[i]:=Rows[i+1];
      RowCount:=RowCount-1;
      if RowNum > stglszd.RowCount-stglszd.FixedCols then
        stglszd.Row:= stglszd.RowCount-stglszd.FixedCols;
    end;
  end;
end;stglszd是一个stringgrid.当最后一条记录按上前头时,stringgrid删除一行,但光标仍然留在被删除行位置,如果用鼠标点击该stringgrid,光标才回到当前最后一行。该怎么解决?

解决方案 »

  1.   

    //删除TStringGrid类中指定行.
    {25}procedure DeleteRow(STringGrid: TSTringGrid; Row: integer);
        var
            I_row: Integer;
            I_col: Integer;
        begin
            if (StringGrid.RowCount-1)=Row then           //如果是最后1行
               For I_col:=1 to StringGrid.ColCount -1 do
                   StringGrid.Cells[I_col,Row]:='';
            //----------------------------------------------------------------------
            for I_row:=Row to StringGrid.RowCount-1 do
            begin
                For I_col:=1 to StringGrid.ColCount-1 do
                begin
                   if I_row=StringGrid.RowCount-1 then    //到最后1行时
                      STringGrid.Cells[I_col,I_row]:=''
                   else
                   begin
                      STringGrid.Cells[I_col,I_row]:=STringGrid.Cells[I_col,I_row+1];
                   end;
                end;
            end;
        end;
      

  2.   

    经查实,原因是设置了stringGrid为可编写,当程序删除最后一行时,stringGrid中的in-place Editor 仍然留在原来行中,还有一个问题:就是当我用rowcount:=rowcount+1追加一行时,原先删除行的内容又出来了,怎么会这样?应该如何解决这2个问题?????
      

  3.   

    rowcount:=rowcount+1;
    stringgrid.cells[0,0] = '';
    stringgrid.cells[0,1] = '';
    ...