2009-09-30 73 views

回答

4

在適當的TDBGrid Key事件中,爲DEL添加陷阱。檢測到時,請檢查您是否在查找列中。如果是,請在相應的數據集字段中調用清除。

+0

如何判斷我是否在查找列中? – croceldon 2009-09-30 20:15:09

+0

閱讀DBGrid.SelectedIndex或甚至更好的DBGrid.SelectedField,它將直接爲您提供字段對象爲Clear()。 – 2009-09-30 21:03:36

+1

那真的很接近。我打電話像grid.SelectedField.Clear,但它似乎並沒有工作。該字段在網格中不受影響。 – croceldon 2009-10-01 14:46:05

1

嘗試在查找表中輸入空白值。

+0

我想到了 - 肯定有一種方法可以做到這一點,而無需添加額外的數據......? – croceldon 2009-09-30 18:48:10

+2

不是我所知道的。查找的重點是根據在其他地方定義的列表給出選擇。如果您開始添加不在該列表中的選項,則不再進行查找。 – 2009-09-30 19:19:01

1

這是我在Delphi XE7中的代碼。訣竅是將查找字段引用的KeyField而不是查找字段本身設爲NULL。

請注意,一旦顯示下拉框,它就不起作用。雙擊單元格時(無論是否放棄),或者如果網格的選項包含dgAlwaysShowEditor。在這種情況下,按鍵不會傳遞給事件處理程序。如果我得到這個工作,我會更新代碼。 (任何想法???)

procedure TformMain.DBGridKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState); 
var 
    LookupResultField : TField; 
begin 
    if Key = VK_DELETE then 
    if TDBGrid(Sender).SelectedField.FieldKind = fkLookup then begin // if this field is a lookup field 

     if not (TDBGrid(Sender).DataSource.DataSet.State in [dsInsert, dsEdit]) then // if the query is not already in edit mode 
     TDBGrid(Sender).DataSource.DataSet.Edit; 

     LookupResultField := TDBGrid(Sender).DataSource.DataSet.FieldByName (TDBGrid(Sender).SelectedField.KeyFields); 
     LookupResultField.Clear;   // set the field to NULL 

    end; 
end; 
+0

無處不在(發件人),我得到錯誤E2066運算符或分號丟失。所以我不能建立這個代碼。 Delphi 10西雅圖。你如何爲你工作? – diedie2 2016-03-16 16:32:51

+0

@ diedie2,只是檢查 - 你有這個作爲你的DBGrid的OnKeyDown事件處理程序? 我剛剛試過編譯它在Delphi 10西雅圖,我沒有得到任何錯誤。當您在屬性檢查器中雙擊OnKeyDown事件時,是否會將您帶入代碼中的此過程或其他位置? – DaveBoltman 2016-03-17 08:15:36

+0

@ diedie2,請給我你的代碼 - 粘貼到這裏的評論。另外告訴我你的TDBGrid對象的名稱是什麼? – DaveBoltman 2016-03-17 08:18:16

相關問題