Главная страница
    Top.Mail.Ru    Яндекс.Метрика
Форум: "Начинающим";
Текущий архив: 2007.03.11;
Скачать: [xml.tar.bz2];

Вниз

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;
Скачать: [xml.tar.bz2];

Наверх





Память: 0.45 MB
Время: 0.044 c
2-1171778901
Dik59
2007-02-18 09:08
2007.03.11
Отключение проверок


2-1171637037
DelphiLexx
2007-02-16 17:43
2007.03.11
TDBF создание индексов


3-1166179693
Vlad Oshin
2006-12-15 13:48
2007.03.11
select from select как сделать правильно?


15-1171436167
homm
2007-02-14 09:56
2007.03.11
java script


1-1168968633
Garacio
2007-01-16 20:30
2007.03.11
найти пустые ячейки в третьем столбце ListView по всем строкам





Afrikaans Albanian Arabic Armenian Azerbaijani Basque Belarusian Bulgarian Catalan Chinese (Simplified) Chinese (Traditional) Croatian Czech Danish Dutch English Estonian Filipino Finnish French
Galician Georgian German Greek Haitian Creole Hebrew Hindi Hungarian Icelandic Indonesian Irish Italian Japanese Korean Latvian Lithuanian Macedonian Malay Maltese Norwegian
Persian Polish Portuguese Romanian Russian Serbian Slovak Slovenian Spanish Swahili Swedish Thai Turkish Ukrainian Urdu Vietnamese Welsh Yiddish Bengali Bosnian
Cebuano Esperanto Gujarati Hausa Hmong Igbo Javanese Kannada Khmer Lao Latin Maori Marathi Mongolian Nepali Punjabi Somali Tamil Telugu Yoruba
Zulu
Английский Французский Немецкий Итальянский Португальский Русский Испанский