Top.Mail.Ru
Мастера DELPHI, Delphi programming community Титульная страница Поиск, карта сайта Написать письмо 
| Новости | Поиск | FAQ | Статьи | Книги | Новости VCL |
| Форумы ЧАТ | Орешник | KOL и MCK |
Форумы -» Основная («Начинающим»)/ Базы / WinAPI / Компоненты / Сети / Media / Игры / Corba и COM / KOL / FreePascal / .Net / Прочее / RSDN.ORG

« предыдущая ветвь | форум | следующая ветвь »

DBGrid [D6, D7]


НОВИЧЕК2007 ©   (2007-02-17 21:37:00) 

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


MsGuns   (2007-02-18 16:07:00) [1]

Из хэлпа:

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.


НОВИЧЕК2007   (2007-02-18 14:29:00) [2]

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


fd979   (2007-02-17 22:36:00) [3]


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

Согласен.


MsGuns   (2007-02-17 21:59:00) [4]

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

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

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

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


fd979   (2007-02-17 21:52:00) [5]

обрабатываешь 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 Имя поля.

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


Наверх

Титульная страница Поиск, карта сайта Написать письмо