2017年9月7日 星期四

DBGrid Draw RowID

 type
  TDataGrid = class(TDBGrid)
    protected
      procedure SetColumnAttributes; override;
  end;


procedure TDataGrid.SetColumnAttributes;
var sStr:String;
  iTextWidth, iCount:Integer;
begin
  inherited;
  //調整標示列的寬度
  iCount := 0;
  if Assigned(Self.DataSource) and (Self.DataSource.DataSet.Active) then
    iCount := Self.DataSource.DataSet.RecordCount;
  sStr := IntToStr(iCount);
  iTextWidth := Self.Canvas.TextWidth(sStr);
  Self.ColWidths[0] := iTextWidth + 15;
end;



procedure TForm1.Grid1DrawDataCell(Sender: TObject; const Rect: TRect;
  Field: TField; State: TGridDrawState);
var sStr:String;
  iPos, i:Integer;
  vRect:TRect;
begin
  //RowID
  if TDataGrid(Sender).DataSource.DataSet.State <> dsInsert then
  begin
    i := TDataGrid(Sender).DataSource.DataSet.RecNo;
    sStr := IntToStr(i);
    vRect := TDataGrid(Sender).CellRect(0, TDataGrid(Sender).Row);
    TDataGrid(Sender).Canvas.TextOut(vRect.Left+2, Rect.Top+2, sStr);
  end;

end;

沒有留言:

張貼留言