DBGrid has a Columns property - a collection of TColumn objects representing all of the columns in a grid control. You can use the Columns editor to set up column attributes at design time, or use the Columns property of the grid to access the properties, events, and methods of TDBGridColumns at runtime.
By default, a user can customize (at run-time) the width and the order of the columns by dragging and dropping column titles. Of course, reordering the columns of a grid does not affect the physical order of the fields in the dataset connected to the grid.
The Options property of a DBGrid component has the goColResizing flag set on by default - thus, allowing the user to resize and reorder the columns of a Grid.
To prevent a user from rearranging columns at run-time, you can either remove the goColResizing flag from Options or set the DragMode property to dmAutomatic (dmManual by default).
Note: here's how to allow column resize but disable movement
Save and Load DBGrid Column Position and WidthIf you allow a user to customize DBGrid's appearance at run time (Columns width and position), when the form is closed and reopened, all the user changes are lost.
For a better user experience, it would be ideal if your application could somehow store (and load when needed) all the changes the user has made to the Grid appearance.
Two simple functions exposed by the TDBGridColumns class: LoadFromFile and SaveToFile help you save and restore the position and width of all the columns in the grid.
Let's say you have a TDBGrid component named "DBGrid1" on a Delphi form, with resizing and reordering of columns enabled at run-time (again, this is by default).
To save the columns order and width (along with other properties like Color, Visible, Title, etc) you simply make the following call:
The SaveToFile method expects one parameter: the name of the file where TDBGridColumns object is saved. Note: a FileStream class is used internally by Delphi to save the TDBGridColumns object.
Note: you could call this method before the Form hosting the grid is closed, ideally in the OnClose event.
When a Form is opened hosting a DBGrid, you could read the saved TDBGridColumns and "fix" the display. Only one line of code is required:
Note: In real world applications, you would need to take sure that the file named passed to both the SaveToFile and ReadFromFile is unique for each grid in the application.
One idea is to construct the file named by concatenating a Form's name, DataSet's name and DBGrid's name. For example : "InvoicesForm_InvoicesQuery_DisplayGrid", where "InvoicesForm" is the name (Name property) of the Form, "InvoicesQuery" is the name od the TDataset object connected to "DisplayGrid" DBGrid.
Programmatically Arranging and Hiding/Unhiding Grid ColumnsA sample application accompanying this article shows how to display all the columns in a CheckBoxList Delphi component to allow a user to hide or show a column by setting the Visible property of a TColumn object. Also, the code shows how to programmatically change the order of columns by rearranging the Items ("connected" to DBGird columns) of the Check Box List.
As you will see, you just need to set the column's Index property to change its position at run-time.
In the sample project, a two way sync is accomplished. When a user moves a column in the Grid, the item in the CheckBox list moves also. The OnColumnMoved event of a DBGrid is used here. Also, a user can rearrange the columns by dragging and dropping CheckBoxList items.