Вниз
Скачать: CL | DM;

DBGrid   Найти похожие ветки 

← →
НОВИЧЕК2007   (2007-02-17 21:37) [0]

Как сделать чтобы в DBGrid по щелчку на заголовке столбца сортировались данные?


← →
fd979 ©   (2007-02-17 21:52) [1]

обрабатываешь OnTitleClick

Var
 S, S1: String;
begin
 S1 := Column.FieldName;
 DBGrid1.DataSource := nil;
 S := UpperCase(Query1.SQL.Strings[Query1.SQL.Count - 1]);
 Query1.Active := False;
 If pos(UpperCase(S1), S) <> 0 then
 begin
   If pos(" DESC ", S) <> 0 then //для того, чтобы не путал с полем в названии которого есть DESC.
     Query1.SQL.Strings[Query1.SQL.Count - 1] := copy(S, 1, Length(S) - 5)
   else
     Query1.SQL.Strings[Query1.SQL.Count - 1] := S + " DESC "
 end
 else
 begin
   Query1.SQL.Strings[Query1.SQL.Count - 1] := "Order by " + S1;
 end;
 Query1.Active := True;
 DBGrid1.DataSource := DataSource1;


Только учти, что будет работать, если у тебя последние строчки SQL будут Order By Имя поля.

При первом нажатии будет сортировать по возростанию, при втором по убыванию


← →
MsGuns ©   (2007-02-17 21:59) [2]

>fd979 ©   (17.02.07 21:52) [1]

Такой код "сломается" на перволм же нелинейном запросе

>НОВИЧЕК2007   (17.02.07 21:37)  

Если используется компоненты ADO, то рекомендую использовать св-во Sort TADOCustomDataSet`а


← →
fd979 ©   (2007-02-17 22:36) [3]


> MsGuns ©   (17.02.07 21:59) [2]
> >fd979 ©   (17.02.07 21:52) [1]
>
> Такой код "сломается" на перволм же нелинейном запросе

Согласен.


← →
НОВИЧЕК2007   (2007-02-18 14:29) [4]

MsGuns ©   (17.02.07 21:59) [2]
Если используется компоненты ADO, то рекомендую использовать св-во Sort TADOCustomDataSet`а
- можете рассказать поподробнее?


← →
MsGuns ©   (2007-02-18 16:07) [5]

Из хэлпа:

Sort property (TCustomADODataSet)

Specifies the sort order of the recordset.

Delphi syntax:

property Sort: WideString;

Description

Set Sort to establish or change the list of fields on which the recordset is sorted. Set sort to the name of a single field or to a comma-separated list of fields. Each field may optionally be followed by the keyword ASCENDING or DESCENDING to specify a sort direction for the field. If one of these keywords is not used, the default sort direction for the field is ascending. Set Sort to an empty string to reset the recordset to the sort order originally used when the recordset’s data was first retrieved.

ADOQuery1.Sort := "LastName ASC, DateDue DESC"

Read Sort to determine the field (or fields) on which the recordset is sorted.

Note: If the cursor is client-side (the dataset component’s CursorLocation property or that of an associated TADOConnection component is clUseClient) and no index already exists matching the requested field sort order, a temporary index is created. Resetting the sort order by setting Sort to an empty string automatically deletes the temporary index.



Страницы: 1 вся ветка

Скачать: CL | DM;



Память: 0.46 MB
Время: 0.038 c
8-1152547191
hgd
2006-07-10 19:59
2007.03.11
Алгоритм заливки


15-1171294889
oldman
2007-02-12 18:41
2007.03.11
Онищенко в очередной раз призвал россиян бросить пить


8-1150456899
!_SM_!
2006-06-16 15:21
2007.03.11
Проиграть звук


2-1171569469
dima1233
2007-02-15 22:57
2007.03.11
Окна


15-1171606301
Loginov Dmitry
2007-02-16 09:11
2007.03.11
Ловушка для сишника MidStr()




   Наверх