How to sort records in Delphi DBGrid [Edit]

I need to alphabetically sort records in the TDBGrid at run-time
I don't want to sort records from the Table or Dataset because that is my main sort index.

I can't seem to work this out can someone help please?  
I am using Delphi 7 it really works for me.

Edited by: unit4 P on Aug 11, 2012 3:10 PM

Edited by: unit4 P on Aug 11, 2012 4:38 PM
0
unit4
8/11/2012 11:38:45 PM
embarcadero.delphi.ide 3925 articles. 0 followers. Follow

19 Replies
3675 Views

Similar Articles

[PageSpeed] 19

On 12/08/2012 9:38 AM, unit4 P wrote:
> I need to alphabetically sort records in the TDBGrid at run-time
> I don't want to sort records from the Table or Dataset because that is my main sort index.
>
> I can't seem to work this out can someone help please?
> I am using Delphi 7 it really works for me.
>
> Edited by: unit4 P on Aug 11, 2012 3:10 PM
>
> Edited by: unit4 P on Aug 11, 2012 4:38 PM
>
Simple - can't do it in the grid.

The third party grids that offer this sort of functionality buffer the 
records in memory and then sort these. The TDBGrid doesn't do this.
You could create your own buffer approach using a MemoryTable or 
ClientDataSet and sort on that....

HTH
Andrew
0
Andrew
8/12/2012 12:11:39 AM
unit4

>I need to alphabetically sort records in the TDBGrid at run-time
>I don't want to sort records from the Table or Dataset because that is my main sort index.
>
>I can't seem to work this out can someone help please?
>I am using Delphi 7 it really works for me.

Is there a problem with creating additional indices on the database? Unless its a very poor database you can have several indices defined and just switch between them as needed.

Roy Lambert
0
Roy
8/12/2012 10:41:25 AM
"unit4 P" wrote in message news:491821@forums.embarcadero.com...
>I need to alphabetically sort records in the TDBGrid at run-time
> I don't want to sort records from the Table or Dataset because that is my 
> main sort index.


The standard TDBGrid does not store data separately from the dataset, it 
doesn't even have the number of rows as in the dataset but is only literally 
a "window" into the datasets rows.

Your choices are:
- Use a ClientDataset/provider (or other available memory datasets) attached 
to your "main" dataset and attach the grid to that
- Use something other that a TDBGrid, such as a DrawGrid/StringGrid or 
Listview - and populate them in code
- Use a 3rd party data-aware grid that does store data or creates indexes of 
its own.

-- 
Wayne Niddery (TeamB)
“The change we seek has always required great struggle and great 
sacrifice.” - Barack Hussein Obama
“The man who speaks to you of sacrifice, speaks of slaves and masters. And 
intends to be the master.” - Ayn Rand
0
Wayne
8/12/2012 2:31:23 PM
unit4,

> I need to alphabetically sort records in the TDBGrid at run-time
> I don't want to sort records from the Table or Dataset because that
> is my main sort index.

You don't mention what DB you are using.  

I think Roy's suggestion is the optimum way to do that task.  Provided,
of course, that your DB provides for multiple indexes.


-- 

   Q

08/12/2012 09:31:05

XanaNews Version 1.19.1.278  [Q'sBrokenToolBar]
0
Quentin
8/12/2012 4:33:48 PM
Wayne,

> Your choices are: ...

Or, as Roy typed, create another index on the data.  Which is "the
right way to do it" in MY mind. <g>

I often have as many as three or four indices on a DB table for various
purposes.  (I use NexusDB.)


-- 

   Q

08/12/2012 09:28:45

XanaNews Version 1.19.1.278  [Q'sBrokenToolBar]
0
Quentin
8/12/2012 4:33:49 PM
Quentin

>I often have as many as three or four indices on a DB table for various
>purposes. (I use NexusDB.)

Just had a quick check and 15 on one table in a current project <g>

Roy Lambert
0
Roy
8/13/2012 6:27:10 AM
You could query the table and select * and sort by the field you need, then connect the grid to the query result.  This would be particularly easy if you only want to view the records.


> {quote:title=unit4 P wrote:}{quote}
> I need to alphabetically sort records in the TDBGrid at run-time
> I don't want to sort records from the Table or Dataset because that is my main sort index.
> 
> I can't seem to work this out can someone help please?  
> I am using Delphi 7 it really works for me.
> 
> Edited by: unit4 P on Aug 11, 2012 3:10 PM
> 
> Edited by: unit4 P on Aug 11, 2012 4:38 PM
0
Trevor
8/13/2012 8:59:02 AM
"Quentin Correll" <qcorrell@pacNObell.net> wrote in message 
news:491913@forums.embarcadero.com...
>
> Or, as Roy typed, create another index on the data.  Which is "the
> right way to do it" in MY mind. <g>
>
> I often have as many as three or four indices on a DB table for various
> purposes.  (I use NexusDB.)


It sounded like he wanted to keep the existing index active - possibly for 
code, e.g. FindKey works against the current index - but display the records 
in a different order. That is a limitation of datasets, only one index can 
be used at a time.

-- 
Wayne Niddery (TeamB)
“The change we seek has always required great struggle and great 
sacrifice.” - Barack Hussein Obama
“The man who speaks to you of sacrifice, speaks of slaves and masters. And 
intends to be the master.” - Ayn Rand
0
Wayne
8/13/2012 1:50:13 PM
Maybe I don't fully understand... but I use the dbgrid in d7 sometimes and have sorting calls ran after the grid gets populate.

I have some queries that work with Oracle to get me my data to populate a dbgrid, then I have some buttons to 
give the end user some ways to sort by.

example
{code}
procedure TForm1.Button1Click(Sender: TObject);
begin
 adoquery1.Sort:='Fieldname1 ASC, Fieldname2 DESC';
end;
{code}

This doesn't seem to cause it to re-query the data. I just tried it, and my initial query took about 10 seconds, then my button code there
sorted it and the grid updated pretty much instantly.
0
steven
8/13/2012 2:41:30 PM
Wayne Niddery wrote:
> "Quentin Correll" <qcorrell@pacNObell.net> wrote in message 
> news:491913@forums.embarcadero.com...
>> Or, as Roy typed, create another index on the data.  Which is "the
>> right way to do it" in MY mind. <g>
>>
>> I often have as many as three or four indices on a DB table for various
>> purposes.  (I use NexusDB.)
> 
> 
> It sounded like he wanted to keep the existing index active - possibly for 
> code, e.g. FindKey works against the current index - but display the records 
> in a different order. That is a limitation of datasets, only one index can 
> be used at a time.
> 

But with client datasets you can setup a second one, call CloneCursor, index the 
second cursor differently and use one for the FindKey and one to drive the grid 
and only have the data once in memory.

-- 
Jeff Overcash (TeamB)
       (Please do not email me directly unless  asked. Thank You)
And so I patrol in the valley of the shadow of the tricolor
I must fear evil. For I am but mortal and mortals can only die.
Asking questions, pleading answers from the nameless
faceless watchers that stalk the carpeted  corridors of Whitehall.
              (Fish)
0
Jeff
8/13/2012 2:45:37 PM
Wayne


>It sounded like he wanted to keep the existing index active - possibly for
>code, e.g. FindKey works against the current index - but display the records
>in a different order. That is a limitation of datasets, only one index can
>be used at a time.

Your interpretation may be right, in which case I feel "unit4 P" has a few conceptual difficulties that are going to bite in the near future <g>

Also don't forget Locate.


Roy Lambert
0
Roy
8/13/2012 3:28:48 PM
steven


But that's a query not a table.

Roy Lambert
0
Roy
8/13/2012 3:47:09 PM
> {quote:title=Roy Lambert wrote:}{quote}
> steven
> 
> 
> But that's a query not a table.
> 
> Roy Lambert

It works the same. Just did a test of it with adotable and ran just the same.
0
steven
8/13/2012 4:17:53 PM
Roy,

> Just had a quick check and 15 on one table in a current project <g>

Makes me a piker!  <giggle>  


-- 

   Q

08/13/2012 09:46:40

XanaNews Version 1.19.1.278  [Q'sBrokenToolBar]
0
Quentin
8/13/2012 4:47:19 PM
Wayne,

> It sounded like he wanted to keep the existing index active -
> possibly for code, e.g. FindKey works against the current index - but
> display the records in a different order. That is a limitation of
> datasets, only one index can be used at a time.

If that is what the OP wants then indeed your comments are correct.  

If so, then depending on the DB (s)he's using (s)he could make another
database-table-datasource for the "other" index activity.

I've done that for a Glossary where there were two "ways" to reference
the Glossary Terms at the same time.  (Using NexusDB.)


-- 

   Q

08/13/2012 09:47:48

XanaNews Version 1.19.1.278  [Q'sBrokenToolBar]
0
Quentin
8/13/2012 4:55:00 PM
Roy,

> Your interpretation may be right, in which case I feel "unit4 P" has
> a few conceptual difficulties that are going to bite in the near
> future <g>

Ditto. <g> 


-- 

   Q

08/13/2012 09:55:43

XanaNews Version 1.19.1.278  [Q'sBrokenToolBar]
0
Quentin
8/13/2012 4:55:53 PM
OK this is what I did:
 All data entered into one table
Then some of the entered data were filtered, let’s say into five order when entered: A, b, c, d, e
I can filter A, b, c, d, e records
But can’t re-sort the records in A anymore because it affects the entire index in the DBGrid
I also wanted to view all records (A, b, c, d or e)  with one DBGrid and with one set of buttons such as: add, delete, edit, new, save

My project tabs goes like this' Delphi, ASP.Net, VB, HTML, PHP, SQL, PIC Basic, Flash' and more
My project used a text highlighter to view the entered code for each order of tabs.
The result or my idea was to keep code for any of the above tabs codes like a directory
Making it ease for me to add, edit, copy and review code that i could not remember for projects.
The columns went like this 'Date updated, Titles, Comments' and the next tab column is where you entered the text code viewed with the text highlighter.  .    

Anyway I was able to solve the problem using the long way by creating separate tables for A, b, c, d, e and so on.
And assigning the variables to one group of buttons and one DBGrid

Thank you all for providing your views and that’s why I love Delphi now Embarcadero and it’s community.   

> {quote:title=unit4 P wrote:}{quote}
> I need to alphabetically sort records in the TDBGrid at run-time
> I don't want to sort records from the Table or Dataset because that is my main sort index.
> 
> I can't seem to work this out can someone help please?  
> I am using Delphi 7 it really works for me.
> 
> Edited by: unit4 P on Aug 11, 2012 3:10 PM
> 
> Edited by: unit4 P on Aug 11, 2012 4:38 PM

Edited by: unit4 P on Oct 5, 2012 3:50 PM

Edited by: unit4 P on Oct 6, 2012 1:21 AM

Edited by: unit4 P on Oct 6, 2012 1:45 AM

Edited by: unit4 P on Oct 6, 2012 1:50 AM
0
unit4
10/6/2012 8:51:14 AM
OK this is what I did:
All data entered into one table
Then some of the entered data were filtered, let’s say into five order when entered: A, b, c, d, e
I can filter A, b, c, d, e records
But can’t re-sort the records in A anymore because it affects the entire index in the DBGrid
I also wanted to view all records (A, b, c, d or e) with one DBGrid and with one set of buttons such as: add, delete, edit, new, save

My project tabs goes like this' Delphi, ASP.Net, VB, HTML, PHP, SQL, PIC Basic, Flash' and more
My project used a text highlighter to view the entered code for each order of tabs.
The result or my idea was to keep code for any of the above tabs codes like a directory
Making it ease for me to add, edit, copy and review code that i could not remember for projects.
The columns went like this 'Date updated, Titles, Comments' and the next tab column is where you entered the text code viewed with the text highlighter. .

Anyway I was able to solve the problem using the long way by creating separate tables for A, b, c, d, e and so on.
And assigning the variables to one group of buttons and one DBGrid

Thank you all for providing your views and that’s why I love Delphi now Embarcadero and it’s community. 

> {quote:title=Quentin Correll wrote:}{quote}
> unit4,
> 
> > I need to alphabetically sort records in the TDBGrid at run-time
> > I don't want to sort records from the Table or Dataset because that
> > is my main sort index.
> 
> You don't mention what DB you are using.  
> 
> I think Roy's suggestion is the optimum way to do that task.  Provided,
> of course, that your DB provides for multiple indexes.
> 
> 
> -- 
> 
>    Q
> 
> 08/12/2012 09:31:05
> 
> XanaNews Version 1.19.1.278  [Q'sBrokenToolBar]
0
unit4
10/12/2012 3:03:12 PM
Sorry i am using Delphi 7 and it's basic database set

Thanks


> {quote:title=unit4 P wrote:}{quote}
> OK this is what I did:
> All data entered into one table
> Then some of the entered data were filtered, let’s say into five order when entered: A, b, c, d, e
> I can filter A, b, c, d, e records
> But can’t re-sort the records in A anymore because it affects the entire index in the DBGrid
> I also wanted to view all records (A, b, c, d or e) with one DBGrid and with one set of buttons such as: add, delete, edit, new, save
> 
> My project tabs goes like this' Delphi, ASP.Net, VB, HTML, PHP, SQL, PIC Basic, Flash' and more
> My project used a text highlighter to view the entered code for each order of tabs.
> The result or my idea was to keep code for any of the above tabs codes like a directory
> Making it ease for me to add, edit, copy and review code that i could not remember for projects.
> The columns went like this 'Date updated, Titles, Comments' and the next tab column is where you entered the text code viewed with the text highlighter. .
> 
> Anyway I was able to solve the problem using the long way by creating separate tables for A, b, c, d, e and so on.
> And assigning the variables to one group of buttons and one DBGrid
> 
> Thank you all for providing your views and that’s why I love Delphi now Embarcadero and it’s community. 
> 
> > {quote:title=Quentin Correll wrote:}{quote}
> > unit4,
> > 
> > > I need to alphabetically sort records in the TDBGrid at run-time
> > > I don't want to sort records from the Table or Dataset because that
> > > is my main sort index.
> > 
> > You don't mention what DB you are using.  
> > 
> > I think Roy's suggestion is the optimum way to do that task.  Provided,
> > of course, that your DB provides for multiple indexes.
> > 
> > 
> > -- 
> > 
> >    Q
> > 
> > 08/12/2012 09:31:05
> > 
> > XanaNews Version 1.19.1.278  [Q'sBrokenToolBar]
0
unit4
10/14/2012 10:38:58 AM
Reply:

Similar Artilces:

SEPA components for Delphi with Source Code (Delphi 5
Hi all, in the european union change next year the Bankingformat to the SEPA Format. All peoples and companies must change the bankingssoftware and the costumer data form acountnummers in the new IBAN and BIC numbers. See: http://www.arma-it.de/shop/artikelueber.php?wgruppeid=211&wgruppe_offen=211 Functions: - generate SEPA XML'S - Calc IBAN - BIC Database (DE,AT and CH) Questions: vertrieb@arma-it.de PS: Bankinssoftware for Develpoers (Germany only) http://www.arma-it.de/shop/artikelueber.php?wgruppeid=212&wgruppe_offen=212 El 26/10/13 21:38, A...

Delphi 7 with ADO DBGrid, entering contents of record into variables. [Edit]
Hi there, For my A-Level computing coursework I'm using a Delphi front-end on an MS Access Database, to store information on customers and create an invoice for them, by calculating data within the database. Basically my problem is this: Is there a way that the user can select a specific record within a DBGrid component, and then have a procedure that will assign the contents of each field for this specific record into a separate variable? E.g. the Record goes like Name: John Smith Date of Birth: 04/09/2005, and when that record is selected, the data contained within nam...

New to Delphi
Hi I am new to Delphi & want to know good resources for learning to use it. Is there a list of good Delphi learning resources (books/online tutorials) somewhere? What recommendations can anyone make for someone new to Delphi (& only a hobby programmer at the moment)? A couple of weeks ago I started with DelphiXE4 trial & have Googled for a few Delphi resources but I haven't found anything very good yet for me as a beginner. I'm just struggling along trying to learn as I go! (I'm presently struggling with DragandDrop...see [http://stackoverflow.com/questions/17232...

Convert Delphi 7 to Delphi 2009 [Edit]
Hi! Please, I have a code done in Delphi 7... Now I want to convert Delphi 7 to Delphi 2009 but I'm having problems when compile the code. Error message: +[DCC Fatal Error] Far.dpr(9): F2063 Could not compile used unit 'System.pas'+ Please can somebody help me? Source Download (349KB): http://rapidshare.com/files/256191328/FileManager.rar.html Mirror Source Download: http://www.megaupload.com/?d=12GYLPT0 Password: delphi Thank u so much! (sorry for my bad english, it is not my native language) Edited by: loquax loquax on Jul 15, 2009 12:24 PM Sorry, bu...

Code works in Delphi 7 but not in Delphi 2010 [Edit]
hello, i have a procedure that open's a file by passing the file name as the parameter to the executable. something like this {code} C : \ P r o g r a m F i l e s \ Da c k e r \ D r a c k e r . e x e " G : \ D E l p h i 7 \ D e l p h i 7 A p p _ l o g . t " {code} The source code is {code} procedure OpenFileWithExe var hReg: HKEY; Ret: Longint; RegDataType, RegDataSize: DWORD; CmdLine: array [0..560] of Char; Len: Integer; SInfo: TStartupInfo; PInfo: TProcessInformation; begin Ret := windows.RegOpenKeyEx(HKEY_CURRENT_USER, ...

Update Delphi 2005 to Delphi 2010, which do i need? [Edit]
Hello, we have one Delphi 2005 Professional here and want to upgrade to Delphi 2010, but we are a bit confused. Our distributor in Germany, ingram-micro offers several upgrade products: UPG DELPHI 2010 PRO - NAMED USER UPG DELPHI 2010 PRO - NAMED USER ELS UPG DELPHI 2010 PRO - NAMED USER FLEX RAD STUDIO 2010 PRO - UPG UPG RAD STUDIO 2010 PRO - NAMED ELC UPG RAD STUDIO 2010 PRO - NAMED FLEX UPG DELPHI 2010 PRO - CONCURRENT ELS UPG DELPHI 2010 PRO - CONCURRENT FLEX What do they mean exactly and which one do i need? At the moment delphi is installed on two PCs and we want to use ...

Migrating from Delphi 6 to Delphi XE 3! [Edit]
All, I am a Delphi developer working in an windows form application developed using Delphi 6. Now, we are planning to upgrade the development tool. Can anyone provide me information related to major roadblocks that we can face while migrating from Delphi 6 to Delphi XE 3? Should we migrate to Delphi XE 3 or any other preferred version of Delphi based on the fact that our target users will be using Windows 7 or Windows 8? Do we have any tools or utilities to migrate the source code from Delphi 6 to higher version of Delphi? Also, any suggestions related to best practices are welcome....

Delphi 7 to Delphi XE: TBlobField to XML [Edit]
Hi, I'm migrating a Delphi7 application to Delphi XE. I'm using a TClientDataSet to communicate, by using a XML frame, with my server. In this TClientDataSet I'm using a TBlobField which is an array of 384 byte. The blobField is allocate by a code like this : {code} myStream : TStream; myStream := aClientDataSet.CreateBlobStream(myBlobField, bmwrite); vResult := myStream.Write(ArrayOf384Byte[0], length(ArrayOf384Byte)); //vResult = 384 => GooD ! (...) {code} For communicate with the server, we have to decode the Blobfield in XML before to sending it. We have...

Delphi vs Winform
This might be a little techie, but I think most people here are familiar with Delphi to understand the difference between a TWinControl and TGraphicControl. I was trying to create a C# Winform project based on a Delphi prototype and ran into this problem which I posted to some Winform forums ... This led me to an *obvious* deficiency in Winforms programming. I'd like to hear your thoughts. My question to the Winforms forums describing the problem is copied below: <snip> Hi, I mostly do Delphi development but would like to start my next small work project in Visual S...

Delphi 2006 vs Delphi 2009 IDE stability
Hello, all. I have medium sized project which includes about 20Mb of source code (just size of *.pas files). I use Delphi 2006. There are third party component suites JVCL, DevExpress Bars, Pegasus Imaging. All updates applied. The main problem is IDE stability. About four..five times a day IDE become unstable after intensive usage of code insight functions (most often I press Ctrl+Space key). The best outcome is that I have to save everything and restart the IDE. The worst outcome is when IDE stalls or falls into endless loop of error messages and any unsaved work is lost. OS...

Delphi and Delphi for .Net
It seems that Delphi for .Net is slower than Delphi Win32 native applicaiton. I would like to know is it true all .Net application is slower than Win32 native applicaiton or it is Delphi for .Net only. Your information is great appreciated, Inung On 2011-06-21 18:20:17 +0100, Inung Huang said: > It seems that Delphi for .Net is slower than Delphi Win32 native applicaiton. > I would like to know is it true all .Net application is slower than > Win32 native applicaiton or it is Delphi for .Net only. If you are only running the code in the application once then, yes, yo...

Delphi 2006 to Delphi XE Crystal BPL problem [Edit]
I am in the process of migrating our application from Delphi 2006 to Delphi XE and, for ease of use, I have both 2006 and XE installed on my development computer. I access Crystal Reports XI RDC in our application and so I rebuilt the BPL in XE in order to add the new package to XE. However, when I try to add the package to XE I get the error message: “Registration procedure, Craxddrt_tbl.Register in the package c:\Documents and Settings\All Uers\Documents\RADS Studio\8.0\BPL\Crystal.bpl raised exception class EFilerError: Component TDatabase can’t be registered by package crystal.bpl be...

ShowMessage/MessagDlg wordwrap Delphi 2006
Hi, I have used Delphi 2006 up to now. ShowMessage allowed a long text without a word wrapping. Now with Delphi XE I notice that a migrated program shows smaller message window sizes and a previously carefully aligned message now appears wrapped and misaligned. How to get back to the previous behaviour? I'm not experienced to create my custom form, so a code snippet may help. Thanks, Uli Edited by: Ulrich Brueggemann on Sep 18, 2010 8:46 AM > I have used Delphi 2006 up to now. > ShowMessage allowed a long text without a word wrapping. > > Now with Delphi ...

Text.Write speed
Hi ! I have speed troubles for using Text.Write method in Delphi 2006, particularly with distant files (local network). For example, with the following code ("F" is a text file ; "Line" is an about 200-character string): ----- AssignFile(F, FileName); try Rewrite(F); for l := 1 to 100 do begin for c := 1 to Length(Line) do Write(F, Line[c]); Writeln(F); end; finally CloseFile(F); end; ----- On a distant file: Delphi 5 -> 0.08 seconds Delphi 2006 -> close to 8 seconds Is there a speci...

Web resources about - How to sort records in Delphi DBGrid [Edit] - embarcadero.delphi.ide

Récord - Wikipedia, the free encyclopedia
Text is available under the Creative Commons Attribution-ShareAlike License ;additional terms may apply. By using this site, you agree to the ...

The Record
Great new podcast co-hosted by Chris Parrish and Brent Simmons: The Record brings you the stories you should know about the Mac and Cocoa development ...

Lompoc Record - Lompoc, California News
The first time Jerry Brown was governor of California, he earned the nickname “Gov. Moonbeam.” It was, after all, the tail-end of the Hippie ...

The App Store in China continues to record strong growth
... concerns in Apple Music , and Apple Watch Herms collection to be available online starting Jan. 22 . The App Store in China continues to record ...

2015 was officially the hottest year on record
... a considerable distance, and a deep glacial lake is rapidly developing in the depression left behind. (credit: Cheryl Strahl ) As record months ...

China Mobile Added Record 4G Subscribers In December
China Mobile Added Record 4G Subscribers In December Barron's (blog) China Mobile (0941.HK/CHL) net added 25 million 4G new subscribers, a ...

Brooklyn Flea holding "mini" Record Fairs at Industry City location in February
... are set up on weekends in the spacious confines of Sunset Park's Industry City. They're going to be having two Brooklyn Flea "mini" Record Fairs ...

2015 was hottest year on record, by wide margin
NOAA says last year scorched through the record books, marking the hottest year since record keeping began

Winter storm: This weekend's snow in DC could be one 'for the record books'
CNN Winter storm: This weekend's snow in DC could be one 'for the record books' CNN (CNN) A nasty snowstorm is expected to bury the Washington, ...

Curry: 'Wasting energy' to think about Bulls' 72-win record
Enter to win prizes during the entire month of January! You can win tickets to a Warriors game, CSN studio tours, a chance to attend the Coaching ...

Resources last updated: 1/21/2016 9:05:52 AM