Вниз
Скачать: 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.03 c
2-1172064977
Cara
2007-02-21 16:36
2007.03.11
Выплывающая форма.


2-1171984783
alll
2007-02-20 18:19
2007.03.11
Удаление компонентов


2-1171666502
Dtitov
2007-02-17 01:55
2007.03.11
При копировании...


15-1171521922
Александр Иванов
2007-02-15 09:45
2007.03.11
Создан квантовый компьютер


2-1171712728
Nija
2007-02-17 14:45
2007.03.11
Нужна срочная помощь




   Наверх