ClientDataSet 排序 ( 积分: 100 )

  • 主题发起人 主题发起人 xushufa
  • 开始时间 开始时间
X

xushufa

Unregistered / Unconfirmed
GUEST, unregistred user!
if Column.Title.SortMarker = smDownEh then
begin
Include(opts, ixDescending);
IdxName := DataSet.Name +Column.Field.FieldName+'DESC';
end
else
begin
IdxName := DataSet.Name +Column.Field.FieldName;
end;

Include(opts, ixCaseInsensitive);
DataSet.IndexName := 'DEFAULT_ORDER';
if DataSet.IndexDefs.IndexOf(IdxName)<> -1 then
begin
DataSet.IndexName:=IdxName;
end
else
begin
DataSet.AddIndex(IdxName, Column.Field.FieldName, opts);
DataSet.IndexDefs.Update;
DataSet.IndexName :=IdxName;

这样的代码执行多次后会在AddIndex地方会出现Key violation,不知道为什么.那位知道怎么解决
 
if Column.Title.SortMarker = smDownEh then
begin
Include(opts, ixDescending);
IdxName := DataSet.Name +Column.Field.FieldName+'DESC';
end
else
begin
IdxName := DataSet.Name +Column.Field.FieldName;
end;

Include(opts, ixCaseInsensitive);
DataSet.IndexName := 'DEFAULT_ORDER';
if DataSet.IndexDefs.IndexOf(IdxName)<> -1 then
begin
DataSet.IndexName:=IdxName;
end
else
begin
DataSet.AddIndex(IdxName, Column.Field.FieldName, opts);
DataSet.IndexDefs.Update;
DataSet.IndexName :=IdxName;

这样的代码执行多次后会在AddIndex地方会出现Key violation,不知道为什么.那位知道怎么解决
 
老大们,发表一下意见
 
procedure TformAddrLst.dbgMainTitleClick(Column: TColumnEh);
Var
sColumn : String;
begin
sColumn := Column.FieldName;
If Column.Title.SortMarker=smDownEh then
begin
Column.Title.SortMarker:=smUpEh;
cdsMain.IndexFieldNames := sColumn ;
End
else
begin
Column.Title.SortMarker:=smDownEh;
cdsMain.AddIndex(sColumn + 'INDEX', sColumn, [ixDescending], sColumn, '', 0);
cdsMain.IndexName := sColumn + 'INDEX';
end;
end;
 
这个代码也是一样的报错出现Key violation
 
你检查一下是否在别的地方已经建立索引了?
 
别的地方是没有建索引,只是有的地方引用了他的数据,并且没有使用这个dataSet,是在服务器端引用的
 
procedure SetGridIndex(DataSet: TDataSet;
Column: TColumnEh);
const
DataSetIndexName = 'FieldIndex';
begin
if not DataSet.Active then
Exit;
if TClientDataSet(DataSet).IndexName <> '' then
TClientDataSet(DataSet).DeleteIndex(DataSetIndexName);
try
if Column.Title.SortMarker = smDownEh then
TClientDataSet(DataSet).AddIndex(DataSetIndexName, Column.FieldName, [])
else
TClientDataSet(DataSet).AddIndex(DataSetIndexName, Column.FieldName, [ixDescending]);
TClientDataSet(DataSet).IndexName := DataSetIndexName;
except
end;
 
procedure SortDBGridEh(Sender: TObject;
ACol: Integer;
Column: TColumnEh);
var
FieldName, SortStr: string;
begin
//TClientdataset(Column.Field.DataSet).IndexFieldNames :=Column.FieldName;
Screen.Cursor := crSQLWait;
try
if (Sender is TDBGridEh) and
((Sender as TDBGridEh).DataSource.DataSet <> nil) then
begin
if not ((Sender as TDBGridEh).DataSource.DataSet is TClientDataSet) then
Exit;
if not (Sender as TDBGridEh).DataSource.DataSet.Active then
Exit;

FieldName := Column.FieldName;
if (Sender as TDBGridEh).DataSource.DataSet.FindField(FieldName).IsBlob then
Exit;
if (Sender as TDBGridEh).DataSource.DataSet.FieldByName(FieldName).FieldKind =
fkData then
SortStr := FieldName
else
if (Sender as TDBGridEh).DataSource.DataSet.FieldByName(FieldName).FieldKind =
fkLookup then
FieldName := (Sender as TDBGridEh).DataSource.DataSet.FieldByName(FieldName).KeyFields
else
FieldName := '';
if (FieldName = '') or (Pos(';', FieldName) > 0) then
Exit;
case Column.Title.SortMarker of
smNoneEh:
begin
Column.Title.SortMarker := smUpEh;
TClientDataSet((Sender as TDBGridEh).DataSource.DataSet).IndexFieldNames :=
FieldName;
end;
smUpEh:
begin
Column.Title.SortMarker := smDownEh;
TClientDataSet((Sender as TDBGridEh).DataSource.DataSet).IndexFieldNames :=
FieldName;// + ' DESC'
end;
smDownEh:
begin
Column.Title.SortMarker := smDownEh;
TClientDataSet((Sender as TDBGridEh).DataSource.DataSet).IndexFieldNames := '';
end;
end;
end;
finally
Screen.Cursor := crDefault;
end;
end;

其他的不用什么设置了,
就可以
你试下吧,我的都可以的
 
写在这个事件里:
DBGridEh1TitleBtnClick(Sender)
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
928
SUNSTONE的Delphi笔记
S
I
回复
0
查看
843
import
I
后退
顶部