StringGrid 快速删除 一行???

okmnji79513 2010-06-03 04:58:10
关于 删除 一行 ,我见到较常见的有如下3种:(来自 http://www.scalabium.com/faq/dct0057.htm )

If you worked with TStringGrid component, then you saw that in this component the Borland developers not provided the method for row deleting. In this tip I describe the few ways for it.

1. navigate by rows and copy the row contains to the prev row:

procedure DeleteRow(yourStringGrid: TStringGrid; ARow: Integer);
var i, j: Integer;
begin
with yourStringGrid do
begin
for i := ARow to RowCount-2 do
for j := 0 to ColCount-1 do
Cells[j, i] := Cells[j, i+1];
RowCount := RowCount - 1
end;
end;

2. the modificated #1:

procedure DeleteRow(yourStringGrid: TStringGrid; ARow: Integer);
var i: Integer;
begin
with yourStringGrid do
begin
for i := ARow to RowCount-2 do
Rows[i].Assign(Rows[i+1]);
RowCount := RowCount - 1
end;
end;

3. the "hacked" way. The TCustomGrid type (the TStringGrid is TCustomGrid's successor) have the DeleteRow method. But this method allocated not in public section but in protected section. So the all successors can "see" this DeleteRow method.

type
THackStringGrid = class(TStringGrid);

procedure DeleteRow(yourStringGrid: TStringGrid; ARow: Integer);
begin
with THackStringGrid(yourStringGrid) do
DeleteRow(ARow);
end;

Personally I use the third method but the first and second are more visual.

就想问一下, 上面 3种 方式,哪种 效率最高,速度最快??(主要是 在行数很多时删除一行,需要速度的最快的方式)

第3种 DeleteRow方式 的源码 看了一下,好像没在 Copy数据 ,是否快一点??Delphi源码我也没看全明白,请大家指点下!!!
或者有其他更快方式??
...全文
486 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
linghengmao 2010-06-04
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 haochin 的回复:]

引用 10 楼 okmnji79513 的回复:
OK 感谢各位。

用 ClientDataSet + DBGrid 是 大家推荐的 操作数据(添加、删除、修改) 效率较高 的方式了吧??
是这样的吗?是的话就准备结贴了。


我没有试过,但听说这样子的组合不错。
[/Quote]
我一般也用这种组合。
okmnji79513 2010-06-04
  • 打赏
  • 举报
回复
Up

还有人有建议吗?
SuperTitan002 2010-06-04
  • 打赏
  • 举报
回复
ClientDataSet1.DisableControls; //不动
ClientDataSet1.EnableControls; //恢复
okmnji79513 2010-06-04
  • 打赏
  • 举报
回复
有个 问题:
用 ClientDataSet + DBGrid 之后,我在查找数据时(比如,查找 某行第二列 的值 等于 X),
当 查找下一条数据时(即 “ClientDataSet1.MoveBy(1)”),光标会动,DBGrid画面也在动(即:一条一条往下走)。
如何能让 DBGrid 的画面不动啊??(就像在操作StringGrid一样??)
haochin 2010-06-03
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 okmnji79513 的回复:]
OK 感谢各位。

用 ClientDataSet + DBGrid 是 大家推荐的 操作数据(添加、删除、修改) 效率较高 的方式了吧??
是这样的吗?是的话就准备结贴了。
[/Quote]

我没有试过,但听说这样子的组合不错。
okmnji79513 2010-06-03
  • 打赏
  • 举报
回复
OK 感谢各位。

用 ClientDataSet + DBGrid 是 大家推荐的 操作数据(添加、删除、修改) 效率较高 的方式了吧??
是这样的吗?是的话就准备结贴了。
SuperTitan002 2010-06-03
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 okmnji79513 的回复:]

引用 7 楼 dinoalex 的回复:
引用 6 楼 okmnji79513 的回复:

引用 5 楼 supertitan002 的回复:
如果数据量很大,不建议你用stringgrid
数据量不大,应该无所谓,呵呵

那 用什么 ? 指条路呢??


cds和dbgrid 一句delete就行了前提是不要执行ApplyUpdates

dbgrid 不连数据库,怎么……
[/Quote]
用缓存也可以啊,只要不执行ApplyUpdates,变化就不会更新到数据库
okmnji79513 2010-06-03
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 dinoalex 的回复:]
引用 6 楼 okmnji79513 的回复:

引用 5 楼 supertitan002 的回复:
如果数据量很大,不建议你用stringgrid
数据量不大,应该无所谓,呵呵

那 用什么 ? 指条路呢??


cds和dbgrid 一句delete就行了前提是不要执行ApplyUpdates
[/Quote]
dbgrid 不连数据库,怎么操作数据啊??不会...
dinoalex 2010-06-03
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 okmnji79513 的回复:]

引用 5 楼 supertitan002 的回复:
如果数据量很大,不建议你用stringgrid
数据量不大,应该无所谓,呵呵

那 用什么 ? 指条路呢??
[/Quote]

cds和dbgrid 一句delete就行了前提是不要执行ApplyUpdates
okmnji79513 2010-06-03
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 supertitan002 的回复:]
如果数据量很大,不建议你用stringgrid
数据量不大,应该无所谓,呵呵
[/Quote]
那 用什么 ? 指条路呢??
SuperTitan002 2010-06-03
  • 打赏
  • 举报
回复
如果数据量很大,不建议你用stringgrid
数据量不大,应该无所谓,呵呵
亮剑_ 2010-06-03
  • 打赏
  • 举报
回复
测试一下,测试结果说明问题
okmnji79513 2010-06-03
  • 打赏
  • 举报
回复
那 是否 3 最快???

还有 更快 的方式不?
休闲中 2010-06-03
  • 打赏
  • 举报
回复
按时间来算:3<2<1
1需要的时间最长
bdmh 2010-06-03
  • 打赏
  • 举报
回复
差不多,你看看Assign的源码 里面有一句AddStrings(TStrings(Source));AddStrings中的代码就跟第一种一样

5,388

社区成员

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

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