Solved

DBGrid Sorting

Posted on 2004-10-15
10
2,185 Views
Last Modified: 2010-08-05
I've been trying to use

procedure TfrmSearch.DBGSearchTitleClick(Column: TColumn);
begin
  if DBGSearch.DataSource.DataSet is TADOQuery then
    if (DBGSearch.DataSource.DataSet as TADOQuery).Sort = Column.FieldName + ' ASC' then
      (DBGSearch.DataSource.DataSet as TADOQuery).Sort := Column.FieldName + ' DESC'
    else
      (DBGSearch.DataSource.DataSet as TADOQuery).Sort := Column.FieldName + ' ASC';
end;

to sort my DBGrid by title when clicking it. However some of the column headings have spaces in (previous programmers code). This (I believe) is causing the sort to fail. I've tried the below as well to try and sort it but I still get the same problem,

procedure TfrmSearch.DBGSearchTitleClick(Column: TColumn);
begin
  if DBGSearch.DataSource.DataSet is TADOQuery then
    if (DBGSearch.DataSource.DataSet as TADOQuery).Sort = '' + Column.FieldName + ' ASC' then
      (DBGSearch.DataSource.DataSet as TADOQuery).Sort :=
                       '' +  Column.FieldName + ' DESC'
    else
      (DBGSearch.DataSource.DataSet as TADOQuery).Sort :=
                      '' +  Column.FieldName + ' ASC';
end;


Any idea's?
0
Comment
Question by:Narusegawa
10 Comments
 
LVL 17

Expert Comment

by:mokule
ID: 12319804
Hi,
double '' is simply an empty string.
Better try ''' before and after field name
0
 

Author Comment

by:Narusegawa
ID: 12319921
''' converts + Column.Fieldname + into a string so the code won't compile then.
0
 

Author Comment

by:Narusegawa
ID: 12319923
I mean it will compile, but " + Column.Fieldname +" would end up being the string itself.
0
 
LVL 17

Accepted Solution

by:
mokule earned 250 total points
ID: 12319959
My mistake.
I've tried such a construction and it works agains SQL Server
ADOTable1.Sort := '[' + ADOTable1.Fields[1].FieldName + ']' + ' ASC';
0
 
LVL 17

Expert Comment

by:Wim ten Brink
ID: 12319994
@mokule, you mean: ''''
Three quotes will make the rest of the line part of the string. ;-)
Four quotes converts to a single quote in one string constant...

However, you don't need quotes to set the sort field.
  DBGSearch.DataSource.DataSet as TADOQuery).Sort := Column.FieldName + ' DESC'
Should be enough. But to make sure, use:
  DBGSearch.DataSource.DataSet as TADOQuery).Sort := '''' + Column.FieldName + ''' ASC'
or
  DBGSearch.DataSource.DataSet as TADOQuery).Sort := '''' + Column.FieldName + ''' DESC'
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 

Author Comment

by:Narusegawa
ID: 12320003
Works a treat! Thanks!
0
 

Author Comment

by:Narusegawa
ID: 12320008
The [ and ] work fine.
0
 
LVL 11

Expert Comment

by:calinutz
ID: 12331941
< Narusegawa > maybe you should give the points to the guys....
0
 
LVL 3

Expert Comment

by:ThoseBug
ID: 12331970
two procedures ready to use from any dbgrid...

var
  FOriginalOptions : TDBGridOptions;

procedure DBGridTitleOrder(Column : TColumn);
{$J+}
 const PreviousColumnIndex : integer = 0;
{$J-}
begin
  if Column.Grid.DataSource.DataSet is TCustomADODataSet then
  with TCustomADODataSet(Column.Grid.DataSource.DataSet) do
  begin
    TDBGrid(Column.Grid).Columns[PreviousColumnIndex].title.Font.Style :=
    TDBGrid(Column.Grid).Columns[PreviousColumnIndex].title.Font.Style - [fsBold];

    Column.title.Font.Style :=
    Column.title.Font.Style + [fsBold];
    PreviousColumnIndex := Column.Index;
    if not (Column.Field.FieldKind = fkLookup) then
    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;

procedure DBGridTitlePointer(Sender : TObject ; x,y : integer);
var
  pt: TGridcoord;
begin
  with TDBGrid(Sender) do
  begin
    pt:= MouseCoord(x,y);
    if pt.y=0 then
      Cursor:=crHandPoint
    else
      Cursor:=crDefault;
  end;
end;

and to use..from the dbgrdis events OnMouseUp.. and OnTitleClick
procedure TForm1.DBGrid1MouseUp(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  DBGridTitlePointer(Sender, x,y);
end;
procedure TForm1.DBGrid1TitleClick(Column: TColumn);
begin
  DBGridTitleOrder(Column);
end;

Cheers. TB.
0
 

Author Comment

by:Narusegawa
ID: 12345174
mokule: That works great on my DBGrid in all it's procedures. (Generic Search Form) Thanks!

workshop_alex: Does that work with columns with spaces in? I could only get it to work with [ and ] around the column name. I've done this in Delphi 6, haven't tested on Delphi 5,7 yet though. And can't test on 8 til it arrives.

calinutz: hehe yeah, sorry about the delay. only have internet at my office so can only come here when I'm in and not rushing out to the bus ^_^ this has to be the best site on the internet, imho
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now