在显示之前修改 DBGrid 单元格内容

发布于 2024-10-16 07:43:49 字数 117 浏览 8 评论 0原文

我想在加载数据库时修改 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

往日情怀 2024-10-23 07:43:49

回到您原来的问题:

使用字段的 OnGetText 事件提供与数据库中存储的值不同的值以用于演示目的。

如果需要将该值呈现给用户,则 DisplayText 布尔参数将为 True;如果需要将该值用于其他目的,则该布尔参数将为 False。

procedure TForm1.SQLQuery1Field1GetText(Sender: TField; 
  var Text: string; DisplayText: Boolean);
begin
  if (Sender.AsString = 'forbidden') and (DisplayText) 
    and (PrivilegeLevel(CurrentUser) < 10) then
    Text := '********'
  else
    Text := Sender.AsString;
end;

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.

procedure TForm1.SQLQuery1Field1GetText(Sender: TField; 
  var Text: string; DisplayText: Boolean);
begin
  if (Sender.AsString = 'forbidden') and (DisplayText) 
    and (PrivilegeLevel(CurrentUser) < 10) then
    Text := '********'
  else
    Text := Sender.AsString;
end;
听风吹 2024-10-23 07:43:49

您可以使用 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

定格我的天空 2024-10-23 07:43:49

使用 DataSet 事件来同步 UI 并不是一个好的做法。您可以依靠 DataSource 事件来做到这一点,将 UI 逻辑与业务逻辑分开。

由于 DataSet 的状态将从 dsInactive 更改为 dsBrowse,因此您可以依靠 DataSource OnState 更改来在从数据库加载数据时进行任何与 UI 相关的操作。

您可以依靠辅助字段来跟踪先前的状态,以避免代码执行超出需要的次数。

例如(未经测试的代码)

procedure TForm1.DataSource1StateChange(Sender: TObject);
begin
  if (DataSource1.State = dsBrowse) and (not FUIStateInSync) then
  begin
    //dataset is open, change UI accordingly
    DBGrid1.Columns[0].Visible := SomeCondition();
    //this will prevent the code to be executed again 
    //as state comes to dsBrowse after posting changes, etc. 
    FUIStateInSync := True; 
  end
  else if (DataSource1.State = dsInactive) then
    FUIStateInSync := False; //to let it happen again when opened.
end;

即使您已经接受了答案,我也会发布此内容,因为 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)

procedure TForm1.DataSource1StateChange(Sender: TObject);
begin
  if (DataSource1.State = dsBrowse) and (not FUIStateInSync) then
  begin
    //dataset is open, change UI accordingly
    DBGrid1.Columns[0].Visible := SomeCondition();
    //this will prevent the code to be executed again 
    //as state comes to dsBrowse after posting changes, etc. 
    FUIStateInSync := True; 
  end
  else if (DataSource1.State = dsInactive) then
    FUIStateInSync := False; //to let it happen again when opened.
end;

I publish this even when you have an accepted answer, because O.D. suggestion is just what you shall avoid.

德意的啸 2024-10-23 07:43:49

连接数据集上的 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

半衬遮猫 2024-10-23 07:43:49

我将修改向网格提供数据的查询,以便不包含具有“禁止”字符串的行(元组)。这似乎比从数据库中检索数据后努力不显示数据要容易得多。

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.

最佳男配角 2024-10-23 07:43:49

我认为最好的方法是不要从 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文