Saturday, August 22, 2020

How to Sort Records in Delphi DBGrid by Column Title

Step by step instructions to Sort Records in Delphi DBGrid by Column Title Delphi DBGrid is such an incredible part, that youre most likely utilizing it consistently if youre creating information mindful applications. Underneath, well investigate how to add some more highlights to your database applications that your clients make certain to cherish. Following the ideas portrayed in the Beginners Guide to Delphi Database Programming, the models beneath use ADO parts (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 part names were left as Delphi named them when dropped on the structure (DBGrid1, ADOQuery1, AdoTable1, and so forth.). Mouse Moves Over DBGrid Title Area In the first place, 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 part. The code beneath essentially utilizes the MouseCoord property of the DBGrid part to ascertain 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 region showing segment/field titles). method TForm1.DBGrid1MouseMove (Sender: TObject; Shift: TShiftState; X, Y: Integer);var pt: TGridcoord;begin pt: DBGrid1.MouseCoord(x, y); in the event 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 advancement, 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 question to have the option to utilize the Sort property. Basically 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 part has a Column parameter showing 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. Subsequently, to sort an ADO dataset by field/segment, a straightforward 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 begin with, we need to, here and there, mark the segment that is presently utilized for sort request. Next, on the off chance that we click on a section title and the dataset is now arranged by that segment, we need to change the sort request from ASC (climbing) to DESC (plummeting), 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 just change the textual style of the segment title to Bold, and evacuate it when dataset is arranged utilizing another segment. method TForm1.DBGrid1TitleClick(Column: TColumn);{$J}const PreviousColumnIndex : 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 safeguard 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.