1. 程式人生 > >怎樣實現在DBGrid中雙擊選擇整行,並且可以多選?謝謝!!

怎樣實現在DBGrid中雙擊選擇整行,並且可以多選?謝謝!!

謝謝 oid next rst enable .get click controls mouseup

DBGrid1->Options裏有個dgMultiSelect,把它設為true就能多選了

先設置DBGrid1->options中dgRowSelect = true, dgMultiSelect = true。
接著寫按下面的寫。
void __fastcall TForm1::DBGrid1DblClick(TObject *Sender)
{
keybd_event(VK_CONTROL,0,0,0); //模擬按下CTRL鍵
}
//---------------------------------------------------------------------------
void __fastcall TForm1::DBGrid1MouseUp(TObject *Sender,
TMouseButton Button, TShiftState Shift, int X, int Y)
{
if(Button==mbRight) //右鍵取消多選;
{
keybd_event(VK_CONTROL,0,2,0); //模擬放開CTRL鍵
}
}

可以實現,不過尚不完美。
在cell點擊時選擇編輯,在dblclick時為整行選定。代碼如下:

void __fastcall TForm1::DBGrid1DblClick(TObject *Sender)
{
TDBGridOptions options = DBGrid1->Options ;
DBGrid1->Options = options << dgRowSelect;

}

void __fastcall TForm1::DBGrid1CellClick(TColumn *Column)
{
TDBGridOptions options = DBGrid1->Options ;
DBGrid1->Options = options >> dgRowSelect;
DBGrid1->Options = options << dgEditing;
}

這樣在每行雙擊時為整行選取,之後點擊每行的非dgTitle部分均為每行選取,點擊cell時為可編輯,單格選取。

procedure TForm1.FormDblClick(Sender: TObject);
begin
  ShowMessage(雙擊);
end;

procedure TForm1.DBGrid1DblClick(Sender: TObject);
begin
  {
  DBGrid1.DataSource.DataSet.DisableControls;
  DBGrid1.DataSource.DataSet.First;
  DBGrid1.SelectedRows.CurrentRowSelected := True;
  DBGrid1.DataSource.DataSet.EnableControls;
  
} keybd_event(VK_Control, MapVirtualKey(VK_Control, 0), 0, 0); keybd_event(VK_Control, MapVirtualKey(VK_Control, 0), KEYEVENTF_KEYUP, 0); //放開Ctrl鍵 //keybd_event(VK_CONTROL,0,0,0); //模擬按下CTRL鍵 // ShowMessage(‘雙擊‘); //DBGrid1.DataSource.DataSet.GetBookmark() //DBGrid1.Options dgmultiselect end; procedure TForm1.Button1Click(Sender: TObject); begin with DBGrid1.DataSource.DataSet do begin DisableControls; First; while not Eof do begin DBGrid1.SelectedRows.CurrentRowSelected := True; Next; end; EnableControls; end; end;

怎樣實現在DBGrid中雙擊選擇整行,並且可以多選?謝謝!!