Saturday, August 22, 2020

How to Sort Records in Delphi DBGrid by Column Title

The most effective method to Sort Records in Delphi DBGrid by Column Title Delphi DBGrid is such a ground-breaking segment, that youre presumably utilizing it consistently if youre creating information mindful applications. Beneath, well investigate how to add some more highlights to your database applications that your clients make certain to adore. Following the ideas depicted in the Beginners Guide to Delphi Database Programming, the models beneath use ADO segments (AdoQuery/AdoTable associated with ADOConnection, DBGrid associated with AdoQuery over DataSource) to show the records from a database table in a DBGrid segment. All the segment names were left as Delphi named them when dropped on the structure (DBGrid1, ADOQuery1, AdoTable1, and so forth.). Mouse Moves Over DBGrid Title Area To start with, lets perceive how to change the mouse pointer while it moves over the DBGrid title region. You should simply add the code to the OnMouseMove occasion for the DBGrid segment. The code underneath essentially utilizes the MouseCoord property of the DBGrid segment to compute where the mouse pointer is. On the off chance that its over the DGBrid title territory, the pt.y rises to 0, which is the main line in the DBGrid (the title zone showing segment/field titles). technique TForm1.DBGrid1MouseMove (Sender: TObject; Shift: TShiftState; X, Y: Integer);var pt: TGridcoord;begin pt: DBGrid1.MouseCoord(x, y); on the off chance that pt.y0, at that point DBGrid1.Cursor:crHandPoint else DBGrid1.Cursor:crDefault;end; Sort on Column Click and Change the Column Title Font In the event that youre utilizing the ADO way to deal with Delphi database improvement, and need to sort the records in the dataset, you have to set the Sort property of your AdoDataset (ADOQuery, AdoTable). The Sort property is the widestring esteem demonstrating the ORDER BY part of the standard SQL inquiry. Obviously, you don't have to compose the SQL inquiry to have the option to utilize the Sort property. Just set the Sort property to the name of a solitary field or to a comma-isolated rundown of fields, each after the sort request. Heres a model: ADOTable1.Sort : Year DESC, ArticleDate ASC The OnTitleClick occasion of the DBGrid segment has a Column parameter demonstrating the Column the client has tapped on. Every Column (object of type TColumn) has a Field property showing the Field (TField) spoke to by the Column, and the Field in its FieldName property holds the name of the field in the basic dataset. Hence, to sort an ADO dataset by field/segment, a basic line can be utilized: with TCustomADODataSet(DBGrid1.DataSource.DataSet) doSort : Column.Field.FieldName;/ASC or DESC The following is the code for the OnTitleClick even handler that sorts the records by segment click. The code, as usual, broadens the thought. To start with, we need to, here and there, mark the section that is as of now utilized for sort request. Next, on the off chance that we click on a segment title and the dataset is as of now arranged by that segment, we need to change the sort request from ASC (climbing) to DESC (sliding), and the other way around. At long last, when we sort the dataset by another section, we need to expel the imprint from the recently chosen segment. For straightforwardness, to stamp the section that sorts the records, well basically change the text style of the segment title to Bold, and evacuate it when dataset is arranged utilizing another segment. technique TForm1.DBGrid1TitleClick(Column: TColumn);{$J}const PreviousColumnIndex : whole number - 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; on the off chance that (Pos(Column.Field.FieldName, Sort) 1) and (Pos( DESC, Sort) 0) at that point Sort : Column.Field.FieldName DESC else Sort : Column.Field.FieldName ASC; end;end; The above code utilizes composed constants to save the estimation of the recently chosen segment for sort request.

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.