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;