Using a DBGrid in Delphi. The 'hooks' between the components are easy to set up using the Object Inspector. See uDatabase.frm below for the settings.
TXDBGrid component is a powerful functional extension of standard TDBGrid component. TXDBGrid component looks beautiful and implements many additional features like: fixed columns, Windows themes and custom styles, many variants of gradient drawing, proportional scrolling for any DataSet even filtered, expandable columns and title headers, checkboxes, hot buttons, title and indicator images, many variants of pictures drawing into cells, sorting markers and multicolumns sorting, enhanced lookup list, data list, calculator, calendar, storing and loading columns layout, enhanced support for bdRightToLeft BiDiMode, extended rows, columns and cells selection, auto-number and auto-select column, hints and tooltips, column stretching and many, many more ...
TXDBGrid offers in addition: auto-updated quick sequence number, auto-changed sorting order for all standard DataSet descendants (ADO, BDE, CDS, DBX, IBX, FireDAC) and many of third-party DAC components, auto-calculated totals values for whole DataSet and/or for SelectedRows, 4 kinds of integrated totals footers, extended lookup columns, universal search panel, integrated filtering system and auto-filter list for each column.
TXDBGrid is fully compatible with standard TDBGrid and can be used as a replacement without losing existing values. TXDBGrid has its own Column Editor and Designer, which can fully support all new column's properties.
TXDBGrid offers in addition: auto-updated quick sequence number, auto-changed sorting order for all standard DataSet descendants (ADO, BDE, CDS, DBX, IBX, FireDAC) and many of third-party DAC components, auto-calculated totals values for whole DataSet and/or for SelectedRows, 4 kinds of integrated totals footers, extended lookup columns, universal search panel, integrated filtering system and auto-filter list for each column.
TXDBGrid is fully compatible with standard TDBGrid and can be used as a replacement without losing existing values. TXDBGrid has its own Column Editor and Designer, which can fully support all new column's properties.
TXDBColumn represents a standalone column editor. TXDBColumn performs the same functionality as inplace editor for the selected column in TXDBGrid component. TXDBColumn directly uses all column's properties set in TXDBGrid. The result of data editing in TXDBColumn is returned to the TXDBGrid.
TXDBEditor represents a standalone field editor. TXDBEditor performs the same functionality for a field in DataSet as inplace editor in TXDBGrid component. TXDBEditor allows to set properties similar to the column properties in TXDBGrid. The result of data editing in TXDBEditor is stored directly to the DataSet. When DataSource property is not assigned TXDBEditor can work as non db-aware editor.
TXDBNavigator represents a gradient database navigator. TXDBNavigator is a functional extension of standard TDBNavigator. TXDBNavigator introduces several new properties to control border of buttons, gradient drawing style and parent's background.
Go to the top of this page
Overview
Main advantages of TXDBGrid:
- Hints and ToolTips for data cells
- Hints for titles, headers, indicator and filler
- User defined markers list (TitleMarkers)
- User defined indicators list (IndicatorImages)
- User defined pictures list for columns (Images)
- Pictures for titles and filler (TitleImages)
- Many variants of pictures drawing into cells (ImageDraw)
- Autotoggled markers for multicolumns sorting (OrderFields)
- Support for fixed columns and styles (FixedCols, FixedStyle)
- Support for bdRightToLeft BiDiMode for Middle Eastern locale
- Support for mouse wheels and drag & drop for selected rows
- Support for save and restore row position in the grid
- Extended rows selection and notification (OnRowSelect)
- Additional options for pick/lookup lists (ListOptions)
- Additional options for PickList/PickText (PickOptions)
- Stretching columns to grid client area (StretchMode)
- Multiline rows, title buttons, headers and many more ...
See also, what new was introduced in version 3.x, 4x, 5.x, 6.x.
Go to the top of this page
Gallery
Below they are located some screenshots of the demo applications included in all packages. Click on selected screenshot to see its bigger version.
TXDBGrid 6.0 |
Classic - zoom | Gradient - zoom | Windows - zoom |
Iceberg - zoom | Amethyst - zoom | Cyan Night - zoom |
Coral - zoom | Emerald - zoom | Metropolis - zoom |
Auric - zoom | Carbon - zoom | Charcoal - zoom |
Custom Styles (RAD Studio XE3, XE4) |
XDBGrid - zoom | XDBGrid - zoom | XDBNavigator - zoom | XDBNavigator - zoom |
Custom Styles (RAD Studio XE2, XE3, XE4) |
XDBGrid - zoom | XDBGrid - zoom | XDBGrid - zoom | XDBGrid - zoom |
XDBNavigator - zoom | XDBNavigator - zoom | XDBControls - zoom | XDBControls - zoom |
X-DBGrid Test for ADO, BDE, IBX |
Test for ADO - zoom | Test for ADO - zoom | Test for BDE - zoom | Test for IBX - zoom |
Classic Windows Style | Windows XP Style |
fsDefault - zoom | fsSoft - zoom | fsNice - zoom | ftDefault - zoom |
fsFlat - zoom | fsGreat - zoom | fsGray - zoom | ftButtons - zoom |
fsFine - zoom | fsMild - zoom | fsMild - zoom | ftTabs - zoom |
Classic Windows Style | Windows XP Style |
DBControls - zoom | Demo #2 - zoom | Demo #3 - zoom | DBControls - zoom |
Windows 8 / 7 / Vista Style | Windows 8 / 7 / Vista |
ftDefault - zoom | ftButtons - zoom | ftTabs - zoom | DBControls - zoom |
History
X-Files Components 1.0 (June 14, 1999). First version for Delphi 4.
X-Files Components 1.1 (July 4, 1999). First version for Delphi 3.
X-Files Components 1.2 (September 9, 1999).
- New Option - dgIndicatorMarkOff
- New properties - ImageOffsetX, ImageOffsetY
- New properties - CheckBox, CheckBoxValues, CheckBoxToggle
- New procedures - IsCheckBoxValue, ToggleCheckBoxValue
X-Files Components 1.3 (October 16, 1999). First version for Delphi 5.
X-Files Components 1.4 (December 18, 1999).
- New Option - dgAllowDeleteOff, dgAllowInsertOff
- New properties - StretchMode, StretchWidthMax, StretchWidthMin, WidthBase
- New properties - ScrollBarHeight, ScrollBarWidth, TrueWidth
- New procedures - StretchGrid
- New events - OnColumnResize, OnColumnScroll, OnRowScroll
X-Files Components 1.5 (March 26, 2000).
- New properties - FillerButton, FillerColor, FixedCols, FixedStyle
- New properties - RowHeight, TitleHeight
- New events - OnFillerClick
X-DBGrid Component 2.0 (June 24, 2000).
- New procedures - HeaderLeft, HeaderRight
- New procedures - DisableGrid, EnableGrid, GridDisabled
- New properties - Report (Visible, TotalType, TotalText, TotalMask)
X-DBGrid Component 2.1 (November 7, 2000).
- New Option - dgMarkerAutoToggle, dgMarkerAscendOnly
- New properties - FillerHint, FillerIndex, FillerPopupMenu, HintOptions, IndicatorImages, IndicatorPopupMenu, IndicatorWidth, OrderFields, TitleImages, TitleMarkers, LastShiftState
- New properties - ColLineWidth, RowLineWidth, Indicators, Markers
- New events - OnCalcImageIndex, OnCellHint, OnIndicatorClick, OnOrderChanged
- New procedures - BeginOrderUpdate, EndOrderUpdate, SetupOrderFields
- New properties - EditorHint, ToolTips, ToolTipsWidth, TransparentColor, Images, AutoToggle, Hint, HeaderHint, HeaderHintRows, ImageIndex, MarkerIndex, OrderIndex
- New procedures - ToggleMarker, UpdateMarkers
- New procedures - ModifyOrderFileds, ExtractOrderFields, CommaToSemicolon, SemicolonToComma
X-DBGrid Component 2.2 (March 21, 2001).
- Context help system
- Custom multiline editor
- Support for BiDiMode property
X-DBGrid Component 2.3 (July 17, 2001). First version for Delphi 6.
X-DBGrid Component 2.4 (November 25, 2001).
- New Options - dgAutoUnselectOff, dgExtendedSelect
- New properties - SelectionAnchor, DragRows
- New procedures - SelectAll, SelectRows, InvertAll, InvertRows, UnselectAll, UnselectRows, ColGetText, ColSetText, DisablePosition, EnablePosition, PositionDisabled
- New events - OnColGetText, OnColSetText
- New properties - ListOptions (loAutoDropDown, loSelectNextValue, loAllowClearValue), PickOptions (poDropDownList, poListItemOnly, poSelectPickText, poStoreItemIndex, poAcceptPickList, poAcceptItemIndex), PickText
X-DBGrid Component 2.5 (March 25, 2002).
- New Options - dgInternalSelect
- New HintOptions- hoDataHints, hoShowDataHints
- New properties - IndicatorHint, Column.Hint
- New events - OnColExpand, OnRowSelect
- New HintCell - hcData, hcIndicator
X-DBGrid Component 2.6 (September 17, 2002). First version for Delphi 7.
X-DBGrid Component 3.0 (September 7, 2003). First version for C++Builder 5 & 6.
- New Options - dgForceSequence, dgThumbTracking, dgHotButtons
- New properties - RecNumber, RecCount, AutoNumber, FixedTheme, GridStyle
- New procedures - ForcedSequence, UpdateSequence, IsGridThemed
- New class - TGridStyle (DataRowSpace, HeaderRowSpace, TitleRowSpace, TitleColMargin, VisualStyle)
X-DBGrid Component 3.2 (March 15, 2004). First version for Delphi 8 for .NET.
X-DBGrid Component 3.3 (July 20, 2004).
- New properties - Ctl3DAuto, DoubleBuffered, EditorColor, FocusRect, HotButtons, ListBorder, MarkerStyle, MarkerTransparent, ResizeOptions, ScrollProp, SelectionColor, SelectRowColor, StripeColor, WheelScrollRows, OnExpandClick, FlatSBMode, Position, DataRowCount, ColumnsWidth, FixedColsWidth
- New methods - ColumnAtDepth, DefaultEditorColor, DefaultSelectionColor, DefaultSelectRowColor, DefaultStripeColor, DropDownMenu, DropDownPoint, GotoPosition, MouseWheelScrollRows, ThemesEnabled
- New properties - CheckBoxKind, CheckBoxStyle, DialogOptions, DropDownMenu, DropDownWidth, ExpandCols, ExpandStyle, Visibility, Report.DialogOptions, Title.DropDownMenu
- New class - TXScrollProp(AutoHidden, Color, ParentColor, ParentStyle, Style, ThumbBar, WinXPMode)
X-DBGrid Component 3.4 (October 8, 2004). First version for Delphi 2005 (.NET & Win32).
- New events - OnMouseWheel, OnMouseWheelDown, OnMouseWheelUp, OnMouseActivate
- New method - DefaultSelectCellColor
- New property - SelectCellColor
X-DBGrid Component 3.5 (April 10, 2005).
- New properties - LookupDataSet, LookupKeyField, LookupListFields
- New ButtonStyle- cbsCalculator, cbsCalendar
- New ListOptions- loShowToday, loShowTodayCircle, loShowWeekNumbers
- New event - OnCalcBoldDays
- New unit - XDBFields
- New function - FieldGraphicClass
- New classes - TXBlobField, TXGraphicField
X-DBGrid Component 3.6 (October 1, 2005). First version for BDS 2006.
- New properties - Settings, AutoStretch
- New event - OnLayout
- New method - MoveTo
- New class - TXDBGridSettings
+ properties - Active, FileName, IniFile, Layout, Options, Section
+ methods - ApplyLayout, EraseLayout, LayoutExists, LoadLayout, ReadLayout, SaveLayout, DefaultFileName, DefaultSection - New variables - DefaultDelimiter, DefaultIniFileName, DefaultRegistryKey
- New functions - ScanStr, ScanInt
Go to the top of this page
Customizing the DBGrid Component
In addition to writing new, custom, cellspacing='0' cellpadding='0'>Note
Unlike the generic grid used earlier, a DBGrid is a virtual view on the dataset—there is no relation between the number of rows shown on the screen and the number of rows of data in the dataset. When you scroll up and down through the data records of the dataset, you are not scrolling through the rows of the DBGrid; the rows are stationary, and the data moves from one row to the next to give the appearance of movement. For this reason, the program doesn't try to set the height of an individual row to suit its data; it sets the height of all the data rows to a multiline height value.
This time the control doesn't have to create a custom data link, because it is deriving from a component that already has a complex connection with the data. The new class has a new property to specify the number of lines of text for each row and overrides a few virtual methods:
The constructor sets the default value for the FLinesPerRow field. Here is the set method for the property:
The side effect of changing the number of lines is a call to the LayoutChanged virtual method. The system calls this method frequently when one of the many output parameters changes. In the method's code, the component first calls the inherited version and then sets the height of each row. As a basis for this computation, it uses the same formula as the TCustomDBGrid class: The text height is calculated using the sample word Wg in the current font (this text is used because it includes both a full-height uppercase character and a lowercase letter with a descender). Here's the code:
Warning | Font and TitleFont are the grid defaults, which can be overridden by properties of the individual DBGrid column objects. This component currently ignores those settings. |
The difficult part of this method was getting the final statements right. You can set the Default-RowHeight property, but in that case the title row will probably be too high. First I tried setting the DefaultRowHeight and then the height of the first row, but this approach complicated the code used to compute the number of visible rows in the grid (the read-only VisibleRowCount property). If you specify the number of rows (to avoid having rows hidden beneath the lower edge of the grid), the base class keeps recomputing it. Here's the code used to draw the data, ported from the RecordView component and adapted slightly for the grid:
In this code you can see that if the user displays a single line, the grid uses the standard drawing technique with no output for memo and graphic fields. However, as soon as you increase the line count, you'll see better output.
To see this code in action, run the GridDemo example. This program has two buttons you can use to increase or decrease the row height of the grid, and two more buttons to change the font. This is an important test because the height in pixels of each cell is the height of the font multiplied by the number of lines.