# delphi dbgrid_如何在Delphi DBGrid中对记录进行排序

delphi dbgrid

Delphi DBGrid is such a powerful component that you're probably using it every day if you're developing data-aware applications. Below, we'll take a look at how to add some more features to your database applications that your users are sure to love.

Delphi DBGrid是一个功能强大的组件，如果您正在开发数据感知应用程序，则可能每天都在使用它。 下面，我们将研究如何向您的用户肯定喜欢的数据库应用程序添加更多功能。

Following the concepts described in the Beginners Guide to Delphi Database Programming, the examples below use ADO components (AdoQuery/AdoTable connected to ADOConnection, DBGrid connected to AdoQuery over DataSource) to display the records from a database table in a DBGrid component.

All the component names were left as Delphi named them when dropped on the form (DBGrid1, ADOQuery1, AdoTable1, etc.).

## 鼠标移到DBGrid标题区域上 ( Mouse Moves Over DBGrid Title Area )

First, let's see how to change the mouse pointer while it moves over the DBGrid title area. All you have to do is add the code to the OnMouseMove event for the DBGrid component.

The code below simply uses the MouseCoord property of the DBGrid component to "calculate" where the mouse pointer is. If it's over the DGBrid title area, the pt.y equals 0, which is the first row in the DBGrid (the title area displaying column/field titles).


procedure TForm1.DBGrid1MouseMove (Sender: TObject; Shift: TShiftState; X, Y: Integer);var pt: TGridcoord;begin pt:= DBGrid1.MouseCoord(x, y);if pt.y=0 then DBGrid1.Cursor:=crHandPointelse DBGrid1.Cursor:=crDefault;end;


## 按列排序并更改列标题字体 ( Sort on Column Click and Change the Column Title Font )

If you're using the ADO approach to Delphi database development, and want to sort the records in the dataset, you need to set the Sort property of your AdoDataset (ADOQuery, AdoTable).

The Sort property is the widestring value indicating the "ORDER BY" part of the standard SQL query. Of course, you do not need to write the SQL query to be able to use the Sort property. Simply set the Sort property to the name of a single field or to a comma-separated list of fields, each following the sort order.

Sort属性是一个宽字符串值，它指示标准SQL查询的“ ORDER BY”部分。 当然，您无需编写SQL查询即可使用Sort属性。 只需将“排序”属性设置为单个字段的名称或以逗号分隔的字段列表，每个字段均遵循排序顺序。

Here's an example:


ADOTable1.Sort := 'Year DESC, ArticleDate ASC'


The OnTitleClick event of the DBGrid component has a Column parameter indicating the Column the user has clicked on. Each Column (object of type TColumn) has a Field property indicating the Field (TField) represented by the Column, and the Field in its FieldName property holds the name of the field in the underlying dataset.

DBGrid组件的OnTitleClick事件具有Column参数，指示用户单击了Column。 每个Column(类型为TColumn的对象)都具有Field属性，该属性指示由Column表示的Field(TField)，并且其FieldName属性中的Field保留基础数据集中的字段名称。

Therefore, to sort an ADO dataset by field/column, a simple line can be used:


with TCustomADODataSet(DBGrid1.DataSource.DataSet) doSort := Column.Field.FieldName; // + ' ASC' or ' DESC'


Below is the code for the OnTitleClick even handler that sorts the records by column click. The code, as always, extends the idea.

First, we want to, in some way, mark the column that's currently used for sort order. Next, if we click on a column title and the dataset is already sorted by that column, we want to change the sort order from ASC (ascending) to DESC (descending), and vice versa. Finally, when we sort the dataset by another column, we want to remove the mark from the previously selected column.

For the sake of simplicity, to mark the column that "sorts" the records, we'll simply change the font style of the column title to Bold, and remove it when dataset is sorted using another column.


procedure TForm1.DBGrid1TitleClick(Column: TColumn);{$J+}const PreviousColumnIndex : integer = -1;{$J-}beginif DBGrid1.DataSource.DataSet is TCustomADODataSet thenwith TCustomADODataSet(DBGrid1.DataSource.DataSet) dobegintry DBGrid1.Columns[PreviousColumnIndex].title.Font.Style := DBGrid1.Columns[PreviousColumnIndex].title.Font.Style - [fsBold];exceptend; Column.title.Font.Style :=  Column.title.Font.Style + [fsBold]; PreviousColumnIndex := Column.Index;if (Pos(Column.Field.FieldName, Sort) = 1)and (Pos(' DESC', Sort)= 0) then Sort := Column.Field.FieldName + ' DESC'else Sort := Column.Field.FieldName + ' ASC';end;end;


The above code uses typed constants to preserve the value of the previously "selected" column for sort order.

delphi dbgrid

• 0
点赞
• 0
评论
• 0
收藏
• 一键三连
• 扫一扫，分享海报

03-19 1215

04-15 606
09-07 2571
02-22 3748
11-23 705
06-24 733
06-22 1838
03-01 2044
06-23 1148
11-04 5681