procedure TForm1.Button1Click(Sender: TObject);
begin
  with TStringList.Create do try
    DBGlist.DataSource.DataSet.First;
    while not DBGlist.DataSource.DataSet.Eof do begin
      if DBGlist.SelectedRows.CurrentRowSelected then
        Add(IntToStr(DBGlist.DataSource.DataSet.RecNo));
      DBGlist.DataSource.DataSet.Next;
    end;
    while not DBGlist.DataSource.DataSet.Bof do begin
      if IndexOf(IntToStr(DBGlist.DataSource.DataSet.RecNo)) >= 0 then
        DBGlist.DataSource.DataSet.Delete;
      DBGlist.DataSource.DataSet.Prior;
    end;
  finally
    Free;
  end;
end;

解决方案 »

  1.   

    请教伴水伤心,,stringdbgrid和dbgrid有什么不同?还有一个好象是drawdbgrid,是吗?
    谢谢:)
      

  2.   

    procedure TForm1.Button1Click(Sender: TObject);
    var i:integer;
        booklist:Tbooklist;
        book:TbookStr;
    begin
       book:=DataModule1.adotable1.Book;
       booklist:=DBGrid1.SelectedRows;
       if  booklist.Count>0 then
         begin
           if MessageDlg('是否要删除这些部门。',mtConfirmation,[mbyes,mbno],0)=mryes then
             begin
                for i:=0 to booklist.Count-1 do
                   begin
                     DataModule1.adotable1.Book:=booklist[i];
                     DataModule1.adotable1.Delete;
                    end;
                BookMarkList.Clear;
             end;
         end;
    end;
      

  3.   

    不行啊
    不关用上面那种方法,
    最后还是提示这个错误,
    是不是有什么控件属性设的不对啊
    我用:
    adoconn->adoquery->datasource->dbgrid
      

  4.   

    没有问题,我刚刚又试了一遍,就用DBNavigator就可以
      

  5.   

    我不想用DBNavigator啊,外形太难看:)
    对了,我的数据是从两张表里取出的,
    不知是不是跟这有关。
      

  6.   

    DBNavigator外观太难看了,怎么用啊。
    我的数据是从两个表中取出的,
    是不是跟这个有关???
      

  7.   

    flg
    你说的第三点怎么解决啊?
      

  8.   

    你没有试我的吗?编译不通?执行出错?
    procedure TForm1.Button1Click(Sender: TObject);
    begin
      with TStringList.Create do try
        DBGlist.DataSource.DataSet.First;
        while not DBGlist.DataSource.DataSet.Eof do begin
          if DBGlist.SelectedRows.CurrentRowSelected then
            Add(IntToStr(DBGlist.DataSource.DataSet.RecNo));
          DBGlist.DataSource.DataSet.Next;
        end;
        while not DBGlist.DataSource.DataSet.Bof do begin
          if IndexOf(IntToStr(DBGlist.DataSource.DataSet.RecNo)) >= 0 then
            DBGlist.DataSource.DataSet.Delete;
          DBGlist.DataSource.DataSet.Prior;
        end;
      finally
        Free;
      end;
    end;
      

  9.   

    to zswang :
    你的方法试了,到这句话
          TViewForm1.DBGlist.DataSource.DataSet.Prior;
    就提示:
    ---------------------------
    Debugger Exception Notification
    ---------------------------
    Project Project2.exe raised exception class EOleException with message '缺少更新或刷新的键列信息。'. Process stopped. Use Step or Run to continue.
    ---------------------------
    OK   Help   
    ---------------------------
    我的数据是从两张表里取出的,应该是这个问题吧。不知道怎么解决?
      

  10.   

    不对
    是这句话:
    DBGlist.DataSource.DataSet.Delete;
      

  11.   

    procedure TForm1.TBtnDelClick(Sender: TObject);
    //删除选中的记录
    var
      strSql:string;
    begin
      with TStringList.Create do try
        TViewForm1.DBGlist.DataSource.DataSet.First;
        while not TViewForm1.DBGlist.DataSource.DataSet.Eof do begin
          if TViewForm1.DBGlist.SelectedRows.CurrentRowSelected then
            Add(IntToStr(TViewForm1.DBGlist.DataSource.DataSet.RecNo));
          TViewForm1.DBGlist.DataSource.DataSet.Next;
        end;
        strSql := 'delete * from article where articleID =:id';
        while not TViewForm1.DBGlist.DataSource.DataSet.Bof do begin
          if IndexOf(IntToStr(TViewForm1.DBGlist.DataSource.DataSet.RecNo)) >= 0 then
    //        TViewForm1.DBGlist.DataSource.DataSet.Delete;
              //获得被删除记录的ID(atricleID),并将它删除
              begin
              adoquery1.Close;
              adoquery1.SQL.Clear;
              adoquery1.SQL.Add(strSql);
              adoquery1.Parameters[0].Value := TViewForm1.DBGlist.DataSource.DataSet.Fields[0].Value;
              //adoquery1.Parameters[0].Value := 20;
              adoquery1.ExecSQL;
              end;
          TViewForm1.DBGlist.DataSource.DataSet.Prior;
        end;
        //adoqtv.Refresh;
        //TViewForm1.DBGlist.Refresh;
        //TViewForm1.DBGlist.DataSource.DataSet.FindFirst;
      finally
        Free;
      end;
    end;
    这是我的改进,现在可以删除记录了,但是再刷新DBGRID时
    还是有同样的错误。
      

  12.   

    刷新是怎么刷新的?DBGlist.Refresh;?干脆不那么耍,重新查询一遍就是了