对于只读数据网格视图来说,数据绑定而不是在我自己的代码中加载数据表是一种不好的做法吗?
我在这里使用 DevExpress GridView,但我猜测这个问题具有更广泛的相关性,至少在数据驱动应用程序的 WinForms 世界中是这样。
目前,我通常在显示从数据库新加载的数据时进行数据绑定,这也是我继承的代码库中的常见做法。但是,我经常无法使用格式字符串格式化(更改文本)绑定数据,因此我最终不得不隐藏格式不良的列,添加具有相似名称的未绑定列,并使用隐藏列中的格式化数据动态填充它们(以前的程序员做得更糟,他经常在存储过程中进行格式化,呃!)。我有一种挥之不去的感觉,也许这种方法很糟糕。
所以我正在考虑一种替代方案 - 假设我创建自己的 FormattableGridView 特别是为了显示只读数据。所有列都将从数据表中解除绑定并动态填充,同时保留与数据表本身相同的列名称。如果我想通过网格格式化某些列、某些行或以之字形的方式,我只需直接动态地进行,因为未绑定的网格可以像二维数组一样容易地被弄乱。
这听起来不错,但显然网格视图的数据绑定(其中大多数可能也是只读的)是很常见的事情。我发现网上一直有人提到它。那么,我上面描述的模式是否存在我所不知道的缺点,从而阻止了它的传播?或者在这些情况下数据绑定本身是一种不明智的模式,而我所描述的确实是更好的方法?
编辑:
好的,所以在部分回答我自己的问题时,进一步的研究发现 DevExpress CustomColumnDisplayText 事件显然专门用于数据绑定网格的不受限制的格式化。也许其他类似框架的 gridview 组件中也存在类似的事件。好吧,也许这确实是正确的模式,尽管处理这个问题的事件驱动方式感觉有点奇怪。
I am using DevExpress GridView here, but I am guessing that the issue is relevant more broadly, at least in the WinForms world of data-driven apps.
At present I am usually doing data binding when displaying data freshly loaded from database and this is also a common practice in the code base I inherited. But, often enough I am unable to format (alter the text) the bound data using format strings, so I end up having to hide the poorly formatted columns, add unbound columns with similar names and dynamically populate them with formatted data from the hidden ones (the previous programmer did even worse, he routinely formatted inside stored procedures, ugh!). I get the nagging feeling that maybe this approach just sucks.
So I am thinking about an alternative - suppose I create my own FormattableGridView especially for the purpose of displaying readonly data. All columns will be unbound and populated dynamically from the data table, while preserving the same column names as the data table itself. If I want to format some columns, some rows or in a zig-zag fashion through the grid, I just do it directly dynamically, because the unbound grid can be messed with just as easily as a 2-dimensional array.
This might sound nice and good, but apparently data binding of grid views (most of which presumably are also readonly) is a pretty common thing. I find it mentioned online all the time. So, are there drawbacks that I am ignorant of to the pattern I delineated above that keep it from spreading? Or is the data binding in itself an unwise pattern in these cases whereas what I described is indeed the better way?
EDITED:
ok, so in part answering my own question, further research uncovers DevExpress CustomColumnDisplayText event apparently specifically intended for unrestricted formatting of databound grids. Perhaps similar events exist in gridview components from other similar frameworks. Ok, so maybe that indeed is the proper pattern, although the event-driven way of handling this issue feels a bit weird.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
网格发布 GridView.CustomColumnDisplayText 事件,该事件可用于格式化格式错误的值。我认为,这是完成这项任务的最佳和最简单的解决方案。
The grid publishes the GridView.CustomColumnDisplayText event which can be used to format wrongly formatted values. I think, that this is the best and easiest solution for this task.