求救 cxGrid 拖拽行的实现

sibad_sh 2010-02-24 03:25:33
拖拽的功能只是实现序号

如:
1 小王
2 小强
3 芝麻

拖拽第三行移至第2行后
1 小王
3 芝麻
2 小强
...全文
280 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
静远 2010-02-27
  • 打赏
  • 举报
回复
贴一段代码,这是自定义的一个procedure,因为是多处要用到这个过程。里面还引用的其他一些函数或过程,可以不管。可能有些复杂,也不见得是解决你的问题的很好的方法,仅供参考~
如五楼所说,在数据表里用到了额外的字段来存储序号。

procedure RowMoveTo(ACurIndex, AOrdIndex: Integer;
ATV: TcxGridDBTableView; AKeyColumn, AOrderColumn: TcxGridDBColumn);
var
i, j, ARecordIndex, AStopIndex: Integer;
ArrID: Array of string;
bm: TBookmark;
AKeyName, AOrderName: string;
// P: TFocusedRecordChanged;
begin
if ACurIndex = AOrdIndex then exit;
P := ATV.onFocusedRecordChanged;
ATV.onFocusedRecordChanged := nil;
with ATV.DataController do
begin
SetLength(ArrID, RecordCount);
ARecordIndex := GetRowInfo(ACurIndex).RecordIndex;
ArrID[AOrdIndex] := Values[ARecordIndex, AKeyColumn.Index];

if ACurIndex > AOrdIndex then
begin
i := 0;
j := i;
AStopIndex := RecordCount - 1;
if ACurIndex = AStopIndex then AStopIndex := ACurIndex - 1;
while i <= AStopIndex do
begin
if i = AOrdIndex then Inc(j);
if i = ACurIndex then Inc(i);
ARecordIndex := GetRowInfo(i).RecordIndex;
ArrID[j] := Values[ARecordIndex, AKeyColumn.Index];
Inc(i);
Inc(j);
end
end else
begin
i := RecordCount - 1;
j := i;
AStopIndex := 0;
if ACurIndex = 0 then AStopIndex := 1;
while i >= AStopIndex do
begin
if i = AOrdIndex then Dec(j);
if i = ACurIndex then Dec(i);
ARecordIndex := GetRowInfo(i).RecordIndex;
ArrID[j] := Values[ARecordIndex, AKeyColumn.Index];
Dec(i);
Dec(j);
end
end;
end;

with ATV.DataController.DataSet do
begin
AKeyName := AKeyColumn.DataBinding.FieldName;
AOrderName := AOrderColumn.DataBinding.FieldName;
bm := GetBookmark;
try
DisableControls;
if ACurIndex > AOrdIndex then
begin
for i := ACurIndex downto AOrdIndex do
if Locate(AKeyName, ArrID[i], []) then
begin
Edit;
FieldByName(AOrderName).AsInteger := i + 1;
Post;
end;
end else
begin
for i := ACurIndex to AOrdIndex do
if Locate(AKeyName, ArrID[i], []) then
begin
Edit;
FieldByName(AOrderName).AsInteger := i + 1;
Post;
end;
end;
finally
GotoBookmark(bm);
FreeBookmark(bm);
EnableControls;
end;
end;
SetLength(ArrID, 0);
// gdTvPositioning(ATV, AOrdIndex);
// ATV.onFocusedRecordChanged := P;
end;
静远 2010-02-27
  • 打赏
  • 举报
回复
思路就如四楼和五楼所说的~
静远 2010-02-27
  • 打赏
  • 举报
回复
引用楼主 sibad_sh 的回复:
拖拽的功能只是实现序号

如:
1  小王
2  小强
3  芝麻

拖拽第三行移至第2行后
1  小王
3  芝麻
2  小强

思路就是四楼和五楼所说了
sibad_sh 2010-02-27
  • 打赏
  • 举报
回复
顶顶顶顶顶顶顶顶顶!
sibad_sh 2010-02-26
  • 打赏
  • 举报
回复
顶顶顶顶顶顶顶顶顶!
haochin 2010-02-25
  • 打赏
  • 举报
回复
引用 1 楼 sibad_sh 的回复:
有经验朋友给个提示吧。。。。。
...
sibad_sh 2010-02-25
  • 打赏
  • 举报
回复
兄弟能说具体点吗?
在drag事件中怎么来做,我真的不懂。
tgbd 2010-02-25
  • 打赏
  • 举报
回复
楼主这个要求,实现上除了已有的两个字段外,还需要一个辅助字段,例LOrder(整型),利用这个字段
作为排序的标识,类似:上移、下移或转到第N行。
正如4F所说的,就是交换LORDER的值。

思路:
  假设当前行的LORDER=10,想要移到LORDER=3
  那么:将当前LORDER置一个较大的数值,例999(假设999没有超出表中最大LORDER值)
     将原3<=LORDER<=9 的记录的LORDER+1
     定位至LORDER=999,将其改为3
  即可。
通过CXGRID中LORDER所在列设置排序、并cxgrid中VIEW的 beginUpdate... endUpdate
wxsan 2010-02-25
  • 打赏
  • 举报
回复
引用楼主 sibad_sh 的回复:
拖拽的功能只是实现序号

如:
1  小王
2  小强
3  芝麻

拖拽第三行移至第2行后
1  小王
3  芝麻
2  小强


交换上下两条记录的序号,然后给字段加索引并排序;
sibad_sh 2010-02-24
  • 打赏
  • 举报
回复
顶顶顶顶顶顶顶顶顶!
sibad_sh 2010-02-24
  • 打赏
  • 举报
回复
有经验朋友给个提示吧。。。。。

5,388

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 开发及应用
社区管理员
  • VCL组件开发及应用社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧