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

标签 delphi tdbgrid

我只想在 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/

相关文章:

c++ - Delphi/C++ Builder - 在 TDBGrid 中设置事件/选定行颜色

delphi - 我可以创建一个构造函数来反序列化对象的字符串版本吗?

.net - 如何诊断 COM 可调用包装对象创建失败?

delphi - 有没有办法确定滚动条在 TDBGrid 上是否可见

delphi - 如何获取 TDBGrid 中的列标题?

Delphi TDBGrid 选定行获取值

delphi - Delphi中垂直面板的自动布局

delphi - firemonkey - 如何以小写字母启动移动键盘?

multithreading - 主线程繁忙时在 Delphi 中显示启动屏幕