在显示之前修改 DBGrid 单元格内容
我想在加载数据库时修改 dbgrid 控件中特定单元格的内容。例如,假设我不希望数据库的任何字段在 dbgrid 中显示,如果它等于“forbidden”。我有什么办法可以做到这一点吗?
I want to modify the content of a particular cell in dbgrid control when the database is loaded. For example, lets say I don't want any field of database to be displayed in dbgrid if it is equal to "forbidden". Is there any way that I can do that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
回到您原来的问题:
使用字段的
OnGetText
事件提供与数据库中存储的值不同的值以用于演示目的。如果需要将该值呈现给用户,则
DisplayText
布尔参数将为 True;如果需要将该值用于其他目的,则该布尔参数将为 False。Going to your original question:
Use the
OnGetText
event of the field to provide a different value from what is stored on the database for presentation purposes.The
DisplayText
boolean parameter will be True if the value is required to be presented to the user and will be False if the value is required for other purposes.您可以使用 DataSetNotifyEvent Afteropen
DBGrid.Datasource.Dataset.Afteropen :=
并且可以使用以下命令隐藏字段:
if Condition then
DBGrid.columns[x].visible := false
您可以检查 OnDrawColumnCell 事件的条件,以便覆盖/删除特定单元格中的某些内容
you can use the DataSetNotifyEvent Afteropen
DBGrid.Datasource.Dataset.Afteropen :=
and you can hide fields with:
if Condition then
DBGrid.columns[x].visible := false
alternative you can check the condition on the OnDrawColumnCell event in order to overrite / delete some content in a specific cell
使用 DataSet 事件来同步 UI 并不是一个好的做法。您可以依靠 DataSource 事件来做到这一点,将 UI 逻辑与业务逻辑分开。
由于 DataSet 的状态将从 dsInactive 更改为 dsBrowse,因此您可以依靠 DataSource OnState 更改来在从数据库加载数据时进行任何与 UI 相关的操作。
您可以依靠辅助字段来跟踪先前的状态,以避免代码执行超出需要的次数。
例如(未经测试的代码)
即使您已经接受了答案,我也会发布此内容,因为 OD 建议正是您应该避免的。
Using the DataSet events to synchronize UI is not a good practice. You can rely on DataSource events to do that, separating UI Logic from business logic.
As the state of the DataSet will change from dsInactive to dsBrowse, you can rely on the DataSource OnState change to make anything UI-related upon the data is loaded from database.
You can rely on a Auxiliar field to track previous state to avoid the code executing more than needed.
for example (untested code)
I publish this even when you have an accepted answer, because O.D. suggestion is just what you shall avoid.
连接数据集上的 OnAfterOpen 事件。
获取隐藏字段并将其 Visible 属性设置为 False,您的 dbgrid 将不会显示它们
干杯
Hookup OnAfterOpen event on dataset.
Get the hidden fields and set its Visible property to False and your dbgrid will not display them
Cheers
我将修改向网格提供数据的查询,以便不包含具有“禁止”字符串的行(元组)。这似乎比从数据库中检索数据后努力不显示数据要容易得多。
I would modify the query that provides the data to the grid so as not to include rows (tuples) which have the 'forbidden' string. This seems much easier than trying hard not to display data after it has already been retrieved from the database.
我认为最好的方法是不要从 DATABASE_TABLE 中选择 WHERE SOME_VALUE="forbidden" 的字段
I think that the best way would be not to SELECT the fields WHERE SOME_VALUE="forbidden" FROM the DATABASE_TABLE