DBGrid移动列之后找到指定FieldName的列号

说明

  • 这是两列需要在移动之后确定新的列号
  • 定义DepositIndexViewDetailIndex
  • 在FromCreate中给DepositIndexViewDetailIndex赋初值
  • ColumnMoved中重新设置DepositIndexViewDetailIndex

方式1:根据FromIndex和ToIndex以及DepositIndex和ViewDetailIndex之间的关系来重新设置DepositIndex和ViewDetailIndex

  • (不推荐-当需要处理的列过多的时候,这样的写法会很麻烦,虽然可以通过数组来处理)
procedure TFormSiteReportMain.RzDBGrid1ColumnMoved(Sender: TObject; FromIndex,
  ToIndex: Integer);
begin
  inherited;
  if FromIndex = DepositIndex then
  begin
      DepositIndex := ToIndex;
      if (FromIndex < ViewDetailIndex) and (ViewDetailIndex <= ToIndex)  then
      begin
        ViewDetailIndex := ViewDetailIndex - 1;
      end
      else if (FromIndex > ViewDetailIndex) and (ViewDetailIndex >= ToIndex) then
      begin
        ViewDetailIndex := ViewDetailIndex + 1;
      end;
  end
  else if FromIndex = ViewDetailIndex then
  begin
      ViewDetailIndex := ToIndex;
      if (FromIndex < DepositIndex) and (DepositIndex <= ToIndex)  then
      begin
        DepositIndex := DepositIndex - 1;
      end
      else if (FromIndex > DepositIndex) and (DepositIndex >= ToIndex) then
      begin
        DepositIndex := DepositIndex + 1;
      end;
  end;

end;

方式2(推荐):根据我们的FieldName来获取对应的列号

procedure TFormSiteReportMain.RzDBGrid1ColumnMoved(Sender: TObject; FromIndex,
  ToIndex: Integer);
var
  i: Integer;
  tmpField: string;
begin
  inherited;
  for I := 0 to RzDBGrid1.Columns.Count - 1 do
  begin
    tmpField := RzDBGrid1.Columns[i].FieldName;
    if tmpField = 'Deposit' then
      DepositIndex := i
    else if tmpField = 'ViewDetail' then
      ViewDetailIndex := i;
  end;
end;

将指定行的鼠标手势变为手指

procedure TFormSiteReportMain.RzDBGrid1MouseMove(Sender: TObject;
  Shift: TShiftState; X, Y: Integer);
var
  i: Integer;
  coords: TGridCoord; // 这个结构体中x是列,y是行
begin
  inherited;
  coords := TRzDBGrid(Sender).MouseCoord(x, y); // 根据鼠标位置获得行列
  if (coords.X = DepositIndex) or (coords.X = ViewDetailIndex) then
    TRzDBGrid(Sender).Cursor := crHandPoint
  else
    TRzDBGrid(Sender).Cursor := crDefault;
end;

你可能感兴趣的:(DBGrid移动列之后找到指定FieldName的列号)