Delphi DevExpress TcxDBTimeEdit控件空值(Null)处理方案问询
解决Delphi中DevExpress TcxDBTimeEdit按Delete键清空并设为Null的问题
需求:使用DevExpress TcxDBTimeEdit控件,实现用户鼠标全选时间内容后按Delete键清空控件内容,同时将关联的数据库字段值设为Null。
尝试过的无效代码
曾在组件Exit事件中编写以下代码,但未达到预期效果:
if (cxDBTimeEdit1.Text = '00:00 AM') or (cxDBTimeEdit1.Text = '12:00 AM') or (cxDBTimeEdit1.Text = '') then dmClaimLine.qryClaimLineTimeOfService.AsString:='';
开发环境
- Delphi 2007
- DevExpress 6.50(2009年4月版本)
- Devart SDAC 8.2.9连接MS SQL Server 2017数据库
最终实现代码
通过以下三个事件的配合代码,成功实现需求:
1. KeyDown事件处理Delete按键逻辑
procedure TfrmC420201ClaimLine.tedTimeOfServiceKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState); begin inherited; // 实现选中时间后按Delete键将数据库字段设为Null // 需先解绑DataField、切换焦点再重新绑定,此方式虽看似特殊但有效 if Key = VK_DELETE then begin tedTimeOfService.DataBinding.DataField:=''; with dmClaimLine do begin if (FMode <> 'Assess') then Exit; ClaimLineEditable; dmClaimLine.qryClaimLineTimeOfService.Clear; dmClaimLine.qryClaimLineTimeOfService.AsString:=''; end; edtMediTimeDuration.SetFocus; tedTimeOfService.DataBinding.DataField:='TimeOfService'; end; end;
2. Enter事件同步控件与数据库值
procedure TfrmC420201ClaimLine.tedTimeOfServiceEnter(Sender: TObject); begin inherited; with dmClaimLine do begin if (FMode <> 'Assess') then Exit; ClaimLineEditable; dmClaimLine.qryClaimLineTimeOfService.AsDateTime := tedTimeOfService.Time end; end;
3. PropertiesChange事件同步控件修改到数据库
procedure TfrmC420201ClaimLine.tedTimeOfServicePropertiesChange(Sender: TObject); var EditValueStr, TimeStr: string; begin inherited; if VarIsNull(tedTimeOfService.EditValue) then EditValueStr := 'Null' else EditValueStr := TimeToStr(tedTimeOfService.Time); if VarIsNull(tedTimeOfService.Time) then TimeStr := 'Null' else TimeStr := TimeToStr(tedTimeOfService.Time); with dmClaimLine do begin if (FMode <> 'Assess') then Exit; if (dmClaimLine.qryClaimLineTimeOfService.AsString = '') and (EditValueStr = 'Null') then exit; if dmClaimLine.qryClaimLineTimeOfService.AsDateTime <> tedTimeOfService.Time then begin ClaimLineEditable; dmClaimLine.qryClaimLineTimeOfService.AsDateTime := tedTimeOfService.Time end; end; end;
内容的提问来源于stack exchange,提问作者Allan F
相关产品推荐
相关产品推荐

