2017-06-23 32 views
0

这是一个简单的问题。 如何初始化变量列做出如下声明。如何初始化TColumnEh

procedure TfmSomeForm.grdSomeGridDblClick(Sender: TObject); 
var 
    Column: TColumnEh; 
    IsSomething: Boolean; 
begin 
    inherited; 
    //Initialize Column 

    IsSomething := False; 
    if Column.FieldName = 'SOMETHING' then 
    IsSomething := True; 

初始化列方式

Column := grdSomeGrid.Columns.FindColumnByName('SOMETHING'); 

是没有意义的,可能会导致异常 我要在这里做

procedure TfmSomeForm.grdSomeGridCellClick(Column: TColumnEh); 
begin 
    inherited; 
    FIsSomething := False; 
    if Column.FieldName = 'SOMETHING' then 
    FIsSomething := True; 
end; 

的问题是,我需要此标志onDblClick,我不想让它成为全球。

+1

你用'列:= grdSomeGrid.Columns.FindColumnByName('SOMETHING')''预测什么问题?当然,你应该检查'Column'是否为Nil。 – MartynA

回答

2

你不会说你的grdSomeGrid是哪种数据类型。然而,对于一个普通的TDBGrid,你似乎想要做的事就是在DblClick事件本身中直接做。

procedure TForm1.DBGrid1DblClick(Sender: TObject); 
var 
    ACol : Integer; 
    Pt : TPoint; 
    CellValue : String; 
begin 
    { pick up the mouse cursor pos and convert to DBGrid's internal coordinates } 
    Pt.X := Mouse.CursorPos.X; 
    Pt.Y := Mouse.CursorPos.Y; 
    Pt := DBGrid1.ScreenToClient(Pt); 

{ find the column number of the double-clicked column} 
    ACol := DBGrid1.MouseCoord(Pt.X, Pt.Y).X - 1; 

    if DBGrid1.Columns[ACol].FieldName = 'SOMETHING' then 
    { do what you want} 
end; 

更新:维多利亚有益提到SelectedIndex,这是另一种方式来获得当前列,但我总是设法忘掉它,因为它的名字不包括Column并没有一个直接对应的该行(因为行操作基于书签而非行索引)。

所以,我这样做,我有,因为它让我想起了如何获取活动列和行索引的方式,很容易写一个独立的功能,同时得到这两个,像这样:

type 
    TRowCol = record 
    Row, 
    Col : Integer; 
    end; 

function GetRowCol(Grid : TDBGrid) : TRowCol; 
var 
    Pt : TPoint; 
begin 
    { pick up the mouse cursor pos and convert to DBGrid's internal coordinates } 
    Pt.X := Mouse.CursorPos.X; 
    Pt.Y := Mouse.CursorPos.Y; 
    Pt := Grid.ScreenToClient(Pt); 

    Result.Row := Grid.MouseCoord(Pt.X, Pt.Y).Y; 
    Result.Col := Grid.MouseCoord(Pt.X, Pt.Y).X; 

    { adjust Col value to account for whether the grid has a row indicator } 
    if dgIndicator in Grid.Options then 
    Dec(Result.Col); 

end; 
+0

这是一个自定义网格,但它几乎相同。我关心列索引OnCellClick与全局变量FColumnIndex:= Column.Index;但我会尝试这种方式,看起来更优雅。对不起,我的英语远非完美。谢谢。 –

+1

对于'TDBGrid'也可以帮助[SelectedIndex](http://docwiki.embarcadero.com/Libraries/en/Vcl.DBGrids.TCustomDBGrid.SelectedIndex)。对于[TDBGridEh](http://www.ehlib.com/online-help/frames.html?frmname=topic&frmfile=DBGridEh_TDBGridEh.html)还没有找到类似的。 – Victoria

+1

@维多利亚:确实。顺道祝贺你的1k + – MartynA