Delphi DBGrid 数据排序(ADOQuery、ADOTable、AdoDataSet、Clientdataset、UniQuery、FDQuery)
<p><strong><span style="font-size: 16px">Delphi DBGrid 数据排序(ADOQuery、ADOTable、AdoDataSet、Clientdataset、UniQuery、FDQuery)</span></strong></p><p><strong><span style="font-size: 16px">1、DBGrid 配合ADOQuery 使用</span></strong></p>
<div class="cnblogs_Highlighter">
<pre class="brush:delphi;gutter:true;"><span style="font-size: 16px">procedure TForm1.DBGrid1TitleClick(Column: TColumn);
var
i: integer;
begin
for i := 1 to DBGrid1.Columns.Count do
begin
//恢复所有标题字体为默认
DBGrid1.Columns.Title.Font.Color := clWindowText;
DBGrid1.Columns.Title.Font.Style := [];
end;
if ADOQuery1.Sort <> (Column.FieldName + ' ASC') then //判断原排序方式
begin
ADOQuery1.Sort := Column.FieldName + ' ASC';
Column.Title.Font.Color := clRed; //改变标题行字体为红色,表示当前的排序方式为升序
Column.Title.Font.Style := ;
end
else
begin
ADOQuery1.Sort := Column.FieldName + ' DESC';
Column.Title.Font.Color := clBlue; //改变标题行字体为红色,表示当前的排序方式为降序
Column.Title.Font.Style := ;
end;
end;
</span></pre>
</div>
<p><strong><span style="font-size: 16px">2、DBGrid 配合ADOTable 操作类似</span></strong></p>
<div class="cnblogs_Highlighter">
<pre class="brush:delphi;gutter:true;"><span style="font-size: 16px">procedure TForm1.DBGrid1TitleClick(Column: TColumn);
begin
with ADOTable1 do
begin
if DBGrid1Boolean then
TADOTable(ryADOTable1).Sort := Column.FieldName + ' DESC'
else
TADOTable(ryADOTable1).Sort := Column.FieldName;
DBGrid1Boolean := not (DBGrid1Boolean);
end;
end;
</span></pre>
</div>
<p><strong><span style="font-size: 16px">3、其他参考(AdoDataSet、Clientdataset)</span></strong></p>
<div class="cnblogs_Highlighter">
<pre class="brush:delphi;gutter:true;"><span style="font-size: 16px">//How to Use:
//procedure TForm1.DBGrid1TitleClick(Column: TColumn);
//begin
// GridTitleSort(column);
//end;
procedure GridTitleSort(Column: TColumn);
type
TFieldTypeSet = set of TFieldType;
var
s, cFieldName: string;
i: integer;
DataSet: TDataSet;
GridFieldTypeSet: TFieldTypeSet;
procedure SetTitle;
var
ii: integer;
cStr: string;
c: TColumn;
begin
for ii := 0 to TDBGrid(Column.Grid).Columns.Count - 1 do
begin
c := TDBGrid(Column.Grid).Columns;
cStr := c.Title.Caption;
if (pos('↑', cStr) = 1) or (pos('↓', cStr) = 1) then
begin
Delete(cStr, 1, 2);
c.Title.Caption := cStr;
end;
end;
end;
begin
DataSet := Column.Grid.DataSource.DataSet;
GridFieldTypeSet := ;
if not (Column.Field.DataType in GridFieldTypeSet) then
Exit; //§P&Acirc;_&brvbar;r&not;q&Atilde;&thorn;&laquo;&not;
SetTitle;
if Column.Field.FieldKind = fkLookup then
cFieldName := Column.Field.KeyFields
else if Column.Field.FieldKind = fkCalculated then
cFieldName := Column.Field.KeyFields
else
cFieldName := Column.FieldName;
//=================================AdoDataSet=====================
if DataSet is TCustomADODataSet then
begin
s := TCustomADODataSet(DataSet).Sort;
if s = '' then
begin
s := cFieldName;
Column.Title.Caption := '↑' + Column.Title.Caption;
end
else
begin
if Pos(cFieldName, s) <> 0 then
begin
i := Pos('DESC', s);
if i <= 0 then
begin
s := s + ' DESC';
Column.Title.Caption := '↓' + Column.Title.Caption;
end
else
begin
Column.Title.Caption := '↑' + Column.Title.Caption;
Delete(s, i, 4);
end;
end
else
begin
s := cFieldName;
Column.Title.Caption := '↑' + Column.Title.Caption;
end;
end;
TCustomADODataSet(DataSet).Sort := s;
end
//============================Clientdataset==========================
else if DataSet is TClientDataSet then
begin
if TClientDataSet(DataSet).indexfieldnames <> '' then
begin
i := TClientDataSet(DataSet).IndexDefs.IndexOf('i' + Column.FieldName);
if i = -1 then
begin
with TClientDataSet(DataSet).IndexDefs.AddIndexDef do
begin
Name := 'i' + Column.FieldName;
Fields := Column.FieldName;
DescFields := Column.FieldName;
end;
end;
TClientDataSet(DataSet).IndexFieldNames := '';
TClientDataSet(DataSet).IndexName := 'i' + Column.FieldName;
Column.Title.Caption := '↓' + Column.Title.Caption;
end
else
begin
TClientDataSet(DataSet).IndexName := '';
TClientDataSet(DataSet).IndexFieldNames := column.fieldname;
Column.Title.Caption := '↑' + Column.Title.Caption;
end;
end;
end;
</span></pre>
</div>
<p><span style="font-size: 16px"><strong>4、</strong>如果使用的是 <strong>UniQuery</strong> 操作类似:</span></p>
<p><span style="font-size: 16px">操作 UniQuery 的 IndexFieldNames属性:</span></p>
<div class="cnblogs_Highlighter">
<pre class="brush:delphi;gutter:true;"><span style="font-size: 16px">indexfieldnames:='字段 desc',
</span></pre>
</div>
<p><span style="font-size: 16px"><strong>5、FDQuery</strong> </span></p>
<p> 参考操作 4</p>
<p> </p>
<p> </p>
<p><span style="color: rgba(136, 136, 136, 1)">创建时间:2020.09.11 更新时间:2021.01.22 / 2021.06.16</span></p>
<p> </p>
</div>
<div id="MySignature" role="contentinfo">
博客园 滔Roy https://www.cnblogs.com/guorongtao 希望内容对你有所帮助,谢谢!<br><br>
来源:https://www.cnblogs.com/guorongtao/p/13651678.html
頁:
[1]