Ir al contenido


Foto

[TRUCO DELPHI] Unidad para Colores las filas de un TDBGrid.


  • Por favor identifícate para responder
No hay respuestas en este tema

#1 sir.dev.a.lot

sir.dev.a.lot

    Advanced Member

  • Miembros
  • PipPipPip
  • 545 mensajes
  • Location127.0.0.1

Escrito 10 diciembre 2016 - 04:21

[TRUCO DELPHI] Unidad para Colores las filas de un TDBGrid.


delphi
  1. Unit uDBSDBGrid;
  2.  
  3. interface
  4.  
  5. Uses
  6. Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, DB,
  7. Grids, DBGrids;
  8.  
  9. Type
  10. TDBSDBGrid = Class(TDBGrid)
  11. private
  12. FRowColorEven : TColor;
  13. FRowColorOdd : TColor;
  14.  
  15. procedure SetRowColorEven(Const Value: TColor);
  16. procedure SetRowColorOdd(Const Value: TColor);
  17. protected
  18. procedure DrawColumnCell(Const Rect: TRect; DataCol: Integer;
  19. Column: TColumn; State: TGridDrawState); override;
  20. public
  21. constructor Create(aOwner: TComponent); override;
  22. function AddColumn(Const aFieldName: String;
  23. Const aMaxValue: Double = 0; aFormat: String = ''): TColumn;
  24. published
  25. property RowColorEven : TColor read FRowColorEven write SetRowColorEven;
  26. property RowColorOdd : TColor read FRowColorOdd write SetRowColorOdd;
  27. end;
  28.  
  29. procedure Register;
  30.  
  31. implementation
  32.  
  33. procedure Register;
  34. begin
  35. RegisterComponents('DBS', [TDBSDBGrid]);
  36. end;
  37.  
  38. { TDBSDBGrid }
  39.  
  40. function TDBSDBGrid.AddColumn(Const aFieldName: String;
  41. Const aMaxValue: Double = 0; aFormat: String = ''): TColumn;
  42. begin
  43. Result := Columns.Add;
  44. Result.FieldName := aFieldName;
  45. If (DataSource.DataSet.FieldByName(aFieldName) is TNumericField) Then
  46. begin
  47. If (aFormat = '') Then
  48. aFormat := (DataSource.DataSet.FieldByName(aFieldName) as TNumericField).DisplayFormat
  49. Else
  50. (DataSource.DataSet.FieldByName(aFieldName) as TNumericField).DisplayFormat := aFormat;
  51. If (aMaxValue <> 0) Then
  52. Result.Width := Canvas.TextWidth(FormatFloat(aFormat, aMaxValue)) + 4;
  53. end;
  54. end;
  55.  
  56. constructor TDBSDBGrid.Create(aOwner: TComponent);
  57. begin
  58. inherited;
  59. //---
  60. FRowColorEven := clWindow;
  61. FRowColorOdd := clInfoBk;
  62. end;
  63.  
  64. procedure TDBSDBGrid.DrawColumnCell(Const Rect: TRect; DataCol: Integer;
  65. Column: TColumn; State: TGridDrawState);
  66. begin
  67. inherited;
  68. //---
  69. If (gdSelected in State) And
  70. (Screen.ActiveControl = Self) And
  71. (Not (csDesigning in ComponentState)) Then
  72. //--- nada
  73. Else
  74. If (Color <> Column.Color) Then
  75. Else
  76. begin
  77. If odd(DataSource.DataSet.RecNo) Then
  78. Canvas.Brush.Color := FRowColorOdd
  79. Else
  80. Canvas.Brush.Color := FRowColorEven;
  81. DefaultDrawColumnCell(Rect, DataCol, Column, State);
  82. end;
  83. end;
  84.  
  85. procedure TDBSDBGrid.SetRowColorEven(Const Value: TColor);
  86. begin
  87. FRowColorEven := Value;
  88. Repaint;
  89. end;
  90.  
  91. procedure TDBSDBGrid.SetRowColorOdd(Const Value: TColor);
  92. begin
  93. FRowColorOdd := Value;
  94. Repaint;
  95. end;
  96.  
  97. end.

Saludos!


  • 1