本文介绍了将TDbGrid中的一些单元格设置为可编辑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只想在TDBGrid中编辑一些单元格。在给定的列中,一些但不是所有的单元格将是可编辑的,所以我不能为整列设置Column.ReadOnly,然后以这种方式离开。

I want only some cells editable in a TDBGrid. In a given column, some but not all cells will be editable, so I can't just set Column.ReadOnly for the entire column and then leave it that way.

什么事件最好使用,所以我可以在输入单元格时得到控制。我可以使用TDbGrid.ColumnEnter来捕获水平运动和TDataSet.AfterScroll用于在网格中垂直移动。或者我可以使用TDBGrid.DrawColumnCell(我已经在使用它来改变某些单元格的颜色...)

What events are best to use so I can get control when a cell is entered. I might use TDbGrid.ColumnEnter to catch horizontal movement and TDataSet.AfterScroll for vertical movement in the grid. Or I could perhaps use TDBGrid.DrawColumnCell (which I'm already using to change the color of some cells...)

而且我也遇到了麻烦最好的方法是改变单元格的只读状态。我可以设置底层的TTable.Field.ReadOnly或TDbGrid.Columns []。ReadOnly。

And I'm also having trouble figuring out the best way change the read-only status of a cell. I could set the underlying TTable.Field.ReadOnly, or TDbGrid.Columns[].ReadOnly.

我可以尝试所有上述,但是我依赖在我的测试中确定电网如何实施,并可能忽视某些情况。我想知道VCL是否提供了一种管理这种需求的方法,如果有注意事项等等。

I could experiment with all of the above, but then I'm depending on my testing to determine how the grid is implemented, and might overlook some situation. I'd prefer to know if the VCL provides a way to manage this need, if there are caveats, etc.

相关:,但不能通过键盘处理滚动。

Related: ReadOnly TDBGrid/TwwDBGrid Cell in Delphi?, but doesn't handle scrolling via the keyboard.

推荐答案

您可以覆盖CanEditModify函数并添加您希望的条件。这可以通过创建一个新的组件来添加一个新的事件或只是通过一个interposerclass。

You can override the CanEditModify function and add your wished condition. This can be done by creating a new compoent with adding a new Event or just by an interposerclass.

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.

这篇关于将TDbGrid中的一些单元格设置为可编辑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-21 06:31