Delphi DbgridEh实现鼠标拖动选中列,并使复选框选中

1.先设置表格列的属性

procedure TForm_TaskToDW.InitGrid;
var
  MyCol: TColumnEh;
begin
  with DBGridEh_Task do
  begin
    Columns.Clear;
    MyCol := Columns.Add;
    MyCol.FieldName := 'Select';
    MyCol.Title.Caption := '选择';
    MyCol.Alignment := taCenter;
    MyCol.Width := 50;
    MyCol.ReadOnly := false;

    MyCol := Columns.Add;
    MyCol.FieldName := 'TASK_NAME';
    MyCol.Title.Caption := '任务名称';
    MyCol.Width := 300;
    //MyCol.Alignment := taCenter;
    MyCol.ReadOnly := True;
  end;
end;

2.创建CDS

procedure TForm_TaskToDW.CreateCDSFlds;
begin
  with CDS_Task do
  begin
    FieldDefs.Clear;
    FieldDefs.Add('Select', ftBoolean);
    FieldDefs.Add('TASK_ID', ftString, 36);
    FieldDefs.Add('TASK_NAME', ftString, 120);
    FieldDefs.Add('IS_GEN', ftString, 1);
    CreateDataSet;
  end;
end;

3.通过下面这个方法实现拖动选中复选框

procedure TForm_TaskToDW.SetSelRowState(State: Boolean = True);
var
  I: Integer;
  BookMark: TBookmarkListEh;
begin
  //BookMark := DBGridEh_Task.SelectedRows;  //TBookmarkListEh

  for I := 0 to DBGridEh_Task.SelectedRows.Count -1 do
  begin
    //if DBGridEh_Task.SelectedRows.CurrentRowSelected then //行被选中
    CDS_Task.GotoBookmark(Pointer(DBGridEh_Task.SelectedRows.Items[i]));
    begin
      CDS_Task.Edit;
      CDS_Task.FieldByName('Select').AsBoolean := State;
      CDS_Task.Post;
    end;
  end;
end;

你可能感兴趣的:(Delphi)