DBGrid的Options-〉dgMultiSelect 和dgRowSelect两个属性被设置为True,这时可以在其中同时选中多行,并且选中行呈高亮状态。但如果选中一条或多条记录,然后滚动鼠标滚轮,这时会有另外一条记录被选中,并随鼠标滚轮移动。如何避免这种情况?谢谢!

解决方案 »

  1.   

    不用去掉,实际上移动的那个焦点并没有被选中。你可以用下面两个方法测试一下:procedure TForm1.Button1Click(Sender: TObject);
    begin
      Showmessage(Inttostr(DBGrid1.SelectedRows.Count));
    end;procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
      DataCol: Integer; Column: TColumn; State: TGridDrawState);
    begin
      if gdSelected in State then
        DBGrid1.Canvas.Font.Color:=clRed;
      if gdFocused in State then
        DBGrid1.Canvas.Brush.Color:=clSkyBlue;
      DBGrid1.DefaultDrawColumnCell(Rect,DataCol,Column,State);
    end;