StringGrid
我想在他的第一列输入一个id以后,按回车,自动在第二列显示id 在数据库里对应的name
就这么简单的一件事,没搞定。
procedure TfrmInput.StringGrid1KeyPress(Sender: TObject; var Key: Char);
begin
  with ADOQuery2 do
  begin
    Close;
    SQL.Clear;
    SQL.Add('select Name from StoreForm where ID='''+StringGrid1.Cells[1,CurrentRow]+'''');
    Open;
  end;
  if key=#13 then
  StringGrid1.Cells[2,CurrentRow] := ADOQuery2.FieldByName('Name').AsString;
end;代码也就这么点,currentrow我在这个单元的pravate里面声明了.CurrentRow: Integer
能通过编译,运行的时候出错,正常的运行结果应该是我输入一个id 以后在name 下面自动显示一个名称,就像下面的一样.
id                name                unit
001              计算机                台
可我用了上面的代码结果是:
id                                    unit
001
看明白了吗,该显示的没显示,不该消失的消失了.

解决方案 »

  1.   

    你给CurrentRow赋了正确的值了吗?
      

  2.   

    CurrentRow,设为类变量,然后在StringGrid的OnSelectCell事件给这个变量赋值就行了.
      

  3.   

    procedure TfrmInput.StringGrid1KeyPress(Sender: TObject; var Key: Char);
    begin
      if key=#13 then
      begin
        if StringGrid1.Cells[1,CurrentRow]<>'' then
        begin
          CurrentRow:= CurrentRow+1;
          with ADOQuery2 do
          begin
            Close;
            SQL.Clear;
            SQL.Add('select Name from StoreForm where ID='''+StringGrid1.Cells[1,CurrentRow]+'''');
            Open;
          end;
          if ADOQuery2.FieldByName('Name').AsString='' then
          begin
            MessageDlg('没有您要查询的物料',mtInformation,[mbOk],0);
            StringGrid1.Cells[1,CurrentRow] := '';
          end;
          StringGrid1.Cells[2,CurrentRow] := ADOQuery2.FieldByName('Name').AsString;
        end;  
      end;
    end;
    我这么写了一下,可以,可有新的问题了
    正确的输入数据没有问题,可如果输入错误的数据以后重新输入新的数据就再也不行了,有的时候也提示‘没有您要查询的物料’,有的时候干脆没有反应。