Главная страница
Top.Mail.Ru    Яндекс.Метрика
Текущий архив: 2007.03.11;
Скачать: 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 вся ветка

Текущий архив: 2007.03.11;
Скачать: CL | DM;

Наверх




Память: 0.47 MB
Время: 0.033 c
3-1165992976
VadimSpb
2006-12-13 09:56
2007.03.11
Запись - "фантом"


2-1171959942
Толибас
2007-02-20 11:25
2007.03.11
TdxDBGridLookupColumn


2-1172047781
webpauk
2007-02-21 11:49
2007.03.11
Сообщение для Timage


15-1171255558
Slider007
2007-02-12 07:45
2007.03.11
С днем рождения ! 12 февраля


15-1171518389
TUser
2007-02-15 08:46
2007.03.11
Хранение ссылок на сайты