是这样的,在一个窗口里有主细两个DBGRID,我想添加一个编辑的按钮。我想按下按钮的时候哪个DBGRID获得了焦点,然后就用哪个ADOTABLE部件的EDIT方法。但是我发现因为主表的记录移动一下,细表马上要更新里面相关联的数据,所以最后主表根本无法获得焦点。请问这个问题该怎么解决呢?十分感谢!

解决方案 »

  1.   

    定位光标的位置来实现setfocus试试
      

  2.   


    MainGrid.datasource.dataset.edit;
    MainGrid.SetFocus;
    ....操作
    MainGrid.Post;
      

  3.   

    最好用两个datasource对应两张明细表,对不同的表采取不同的操作。下面是一段明细表保存的代码示例procedure DoPost_Mst(Ms_Tab, LinkId, Dt_Field : String);
    var
      i, j, Get_Link_Id : Integer;
    begin
      with MasAds do // 判断主表的修改/新增状态
        begin
          if FieldByName(LinkId).AsInteger = -1 then
            Get_Link_Id := GetMaxId(Ms_Tab,LinkId)
          else
            Get_Link_Id := FieldByName(LinkId).AsInteger;
        end;
    try     // 从表保存
      with Dt1Ads do
        begin
          if IsEmpty then
            CancelBatch()
          else
            begin
              First;
              for i := 0 to RecordCount - 1 do
                begin
                  Edit;
                  if (FieldByName(LinkId).AsInteger = -1) or
                     (FieldByName(LinkId).IsNull) then
                    FieldByName(LinkId).AsInteger := Get_Link_Id;
                  FieldByName(Dt_Field).AsInteger := i + 1;
                  Post;
                  Next;
                end;
                //  showmessage('出错1');
              UpdateBatch();        end;
        end;
      if Dt2Ads <> Nil then
        begin
          with Dt2Ads do
            begin
              if IsEmpty then
                CancelBatch()
              else
                begin
                  First;
                  for j := 0 to RecordCount - 1 do
                    begin
                      Edit;
                      if (FieldByName(LinkId).AsInteger = -1) or
                         (FieldByName(LinkId).IsNull) then
                        FieldByName(LinkId).AsInteger := Get_Link_Id;
                      FieldByName(Dt_Field).AsInteger := j + 1;
                      Post;
                      Next;
                    end;
                  UpdateBatch();
                end;
            end;
        end;     with MasAds do   //主表保存;
        begin
          if FieldByName(LinkId).AsInteger = -1 then
            begin
         //   showmessage('出错2');
              Edit;
              FieldByName(LinkId).AsInteger := Get_Link_Id;
            end;
          UpdateBatch();
        end;
    Except
     // showmessage('出错3');
      Application.MessageBox('保存数据出现异常,请重试!','系统信息',
          MB_OK+MB_ICONINFORMATION);
    end;
    end;
      

  4.   

    定位光标的位置来实现setfocus
      

  5.   

    设置正确的主从表项后,利用setfocus来定位光标。。位置可能通过属性得到。