在 Delphi 中重新定位 DBGrid 中的列
我需要动态更改 DBGRid 中某些列的位置。假设我需要将列号 21 放在位置 10 上。我使用:
DBGrid.Columns[21].Index:=10;
但是,这也会更改数组本身,这意味着下次我想访问此列时,我将需要编写 DBGrid.Columns[10],这使得它有点不干净,我需要记住所有列的位置等。有没有更简单的方法来重新定位列? 如果在此位置更改期间数组索引不发生更改,那就太好了。
I need to dynamically change position of certain column in DBGRid. Let's say I need to place column number 21 on position 10. I use:
DBGrid.Columns[21].Index:=10;
But, this also changes the array itself, that means, that next time I want to access this column, I will need to write DBGrid.Columns[10], this makes it a little unclean, I need to memorize positions of all columns etc. Is there an easier way to reposition a column?
It would also be good if array indexes do not change during this position change.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
处理该问题的一个简单方法是不按索引访问列,而是按字段名访问列。引入这样的方法:
缺点是每次需要访问网格时都必须运行循环,导致小延迟,因此如果速度很重要,您可能会考虑其他选择。
A simple way to deal with the problem is to not access the columns by index but by fieldname. Introduce a method like this:
The drawback is that you have to run the loop every time you need to access the grid, causing a small delay, so if speed is essential you might consider other options.
无论如何,对于那些到达此页面寻找重新排序网格中列的方法的人(像我一样):
输入列是从零开始的。
Anyway, for those (like me) who reached this page looking for a way to reorder columns in a grid:
Input columns are zero-based.
你是对的。您必须跟踪您的列所在的位置。也许在一个单独的结构中,或者作为从 TCustomGrid 派生的后代对象。
我保留一个容器对象,其中存储列的大小、它们包含的数据类型、排序顺序、格式选项以及网格中的位置等内容。然后我有一个引用容器的自定义网格。
结尾;
You are right. You have to keep track of where your columns are located. Maybe in a separate structure, or as a descendant object derived from TCustomGrid.
I keep a container object, where I store, among other things, the size of the columns, the type of the data they contain, the sort order, formatting options, and the position in the grid. And then I have a custom grid that references the container.
end;