通过输入一个值,在StringGrid表格中查到相应的值,并且选中该单元格。
请问,如何用代码来选中某个单元格?还请各位高手指点,谢谢!

解决方案 »

  1.   

    var
      row, col: Integer;
      sel: TGridRect;
    begin
      for row := 0 to StringGrid1.RowCount - 1 do
      begin
        for col := 0 to StringGrid1.ColCount - 1 do
        begin
          if StringGrid1.Cells[col, row] = Edit1.Text then
          begin
            sel.Left := col;
            sel.Top := row;
            sel.Right := col;
            sel.Bottom := row;
            StringGrid1.Selection := sel;
          end;
        end;
      end;
    end;
      

  2.   


    procedure Tfrmdata.sjgzgridMouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    var
      Column,Row:integer;
    begin
      if Button=mbRight then
      begin
        sjgzgrid.MouseToCell(X,Y,Column, Row);
        sjgzgrid.Col:=Column;
        sjgzgrid.Row:=Row;
      end;
      if Button=mbLeft  then
      begin
        sjgzgrid.MouseToCell(X, Y, Column, Row);
        sjgzgrid.Col:=Column;
        sjgzgrid.Row:=Row;
      end;
    上面的可以控制鼠标选中哪个行,你要选Cell,用2楼的那个TGridRect;