gpt4 book ai didi

delphi - 将 TDbGrid 中的一些单元格设置为可编辑

转载 作者:行者123 更新时间:2023-12-01 16:35:55 25 4
gpt4 key购买 nike

我只想在 TDBGrid 中编辑一些单元格。在给定的列中,某些单元格(但不是所有单元格)都是可编辑的,因此我不能只为整个列设置 Column.ReadOnly,然后就保持这种状态。

最好使用哪些事件,以便我可以在输入单元格时进行控制。我可能使用 TDbGrid.ColumnEnter 捕获水平移动,使用 TDataSet.AfterScroll 捕获网格中的垂直移动。或者我也许可以使用 TDBGrid.DrawColumnCell (我已经用它来更改某些单元格的颜色...)

而且我也无法找出更改单元格只读状态的最佳方法。我可以设置底层 TTable.Field.ReadOnly 或 TDbGrid.Columns[].ReadOnly。

我可以尝试以上所有内容,但随后我会根据测试来确定网格的实现方式,并且可能会忽略某些情况。我更想知道 VCL 是否提供了一种方法来管理这种需求,是否有警告等。

相关:ReadOnly TDBGrid/TwwDBGrid Cell in Delphi? ,但不处理通过键盘的滚动。

最佳答案

您可以重写 CanEditModify 函数并添加您想要的条件。这可以通过创建新组件并添加新事件或仅通过插入器类来完成。

unit Unit6;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, DB, ADODB, Grids, DBGrids;

type
TDBGrid=Class(DBGrids.TDBgrid)
function CanEditModify: Boolean; override;
Property Col; // make property col visible
End;

TForm6 = class(TForm)
DBGrid1: TDBGrid;
ADOConnection1: TADOConnection;
ADODataSet1: TADODataSet;
DataSource1: TDataSource;
ADODataSet1Componame: TStringField;
ADODataSet1TrackTitle: TStringField;
private
{ Private-Deklarationen }
public
{ Public-Deklarationen }
end;

var
Form6: TForm6;

implementation

{$R *.dfm}

{ TDBGrid }

function TDBGrid.CanEditModify: Boolean;
var
f:TField;
c:Integer;
begin
Result := inherited CanEditModify;
c := Col;
if dgIndicator in Options then dec(c);
F := Columns[c].Field;
if Assigned(F) then
begin // here just an example condition
if (f.FieldName='TrackTitle') then
if Pos('aa',F.AsString)>0 then Result := False;
// you also can access the dataset via
// if f.DataSet.FieldByName('xy').SomeCondition then ....
end;
end;

end.

关于delphi - 将 TDbGrid 中的一些单元格设置为可编辑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16042975/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com