【发布时间】:2015-10-02 21:50:26
【问题描述】:
我想将 DBLookupComboboxes 添加到 DBGrid 中的某些列。 About.com 上有一篇关于如何做到这一点的好文章here。问题在于,对于有很多列的表,如果您从 DBLookupCombobox 中选择一列,然后尝试向左滚动,则组合框也会向左移动,如包含的图像所示。如何更改 About.com 代码以防止这种行为?网络搜索显示另外两个人抱怨完全相同的问题但没有解决方案。请注意,我想使用 DBLookupCombobox 来显示名称但输入 id,因此使用简单的选择列表是行不通的。
procedure TForm1.DBGrid1ColExit(Sender: TObject);
begin
if DBGrid1.SelectedField.FieldName = DBLookupComboBox1.DataField then
DBLookupComboBox1.Visible := False
end;
procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
DataCol: Integer; Column: TColumn; State: TGridDrawState);
begin
if (gdFocused in State) then
begin
if (Column.Field.FieldName = DBLookupComboBox1.DataField) then
with DBLookupComboBox1 do
begin
Left := Rect.Left + DBGrid1.Left + 2;
Top := Rect.Top + DBGrid1.Top + 2;
Width := Rect.Right - Rect.Left;
Width := Rect.Right - Rect.Left;
Height := Rect.Bottom - Rect.Top;
Visible := True;
end;
end
end;
procedure TForm1.DBGrid1KeyPress(Sender: TObject; var Key: Char);
begin
if (key = Chr(9)) then Exit;
if (DBGrid1.SelectedField.FieldName = DBLookupComboBox1.DataField) then
begin
DBLookupComboBox1.SetFocus;
SendMessage(DBLookupComboBox1.Handle, WM_Char, word(Key), 0);
end
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
with DBLookupComboBox1 do
begin
DataSource := DataSource1; // -> AdoTable1 -> DBGrid1
ListSource := DataSource2;
DataField := 'resource_id'; // from AdoTable1 - displayed in the DBGrid
KeyField := 'id';
ListField := 'resource_name; id';
Visible := False;
end;
DataSource2.DataSet := AdoQuery1;
AdoQuery1.Connection := AdoConnection1;
AdoQuery1.SQL.Text := 'SELECT id,resource_name FROM resources';
AdoQuery1.Open;
end;
【问题讨论】:
-
About.com 代码的链接不够。我们需要你的代码(一个MCVE)来演示这个问题。它显然可以与 About 上的代码一起使用,因为其他人(包括我)使用它没有问题。
-
那么可能是 Delphi 的问题。我将代码逐字粘贴到一个新项目中(我的数据引用除外),并在 Delphi XE 和 Delphi 2006 上都看到了这种行为。
-
再一次,MCVE 会有所帮助。我在 Delphi 2007 中使用该代码没有问题。我从这里看不到你做了什么。如果您需要有关代码的帮助,请将代码包含在您的问题中。链接到其他人的代码是没有好处的,我们不应该离开这个网站来弄清楚你在问什么。
-
看起来使此方法起作用的唯一方法是创建一个 DBGrid 后代,然后在 TCustomGrid 中显示 WMHScroll 过程。然后,在滚动期间,您可以使用 DBLookupCombobox(或任何其他组件)将焦点从单元格上移开。希望我错了。