I have a strange thing happening with a DBLookupComboBox.
I have set it so the DBLookupComboBox is displayed within a cell of the datagrid. When I change a value, the value displayed in the cell shows only the first 10 characters of the item selected in the DBLookupComboBox. Then if I continually use it, the displayed value becomes only the first four characters of the the item selected.
The problem is specifically related to to the DBLookupComboBox, as the datagrid will display the correct values from the datasource, it is only when you change them via the DBLookupComboBox when problems occur. The database fields are set to varchar(50), so I don't see what the problem is.
When I use this piece of code, it does show the selected item in the DBLookupComboBox has been truncated to 4 characters:
//*******************************************************
procedure TfrmMain.DBLookupComboBox1Exit(Sender: TObject);
var
strSelectedItem : String;
begin
strSelectedItem := DBLookupComboBox1.Field.AsString;
showmessage('Selected Item: ' + strSelecteditem);
end;
//*******************************************************
Here is the rest of my code which sets up the DBLookupComboBox:
//set dbLookupCombobox connection and settings
procedure TfrmMain.setDBLookup;
begin
//set data source, connection, and SQL query for the dbLookupCombobox
DBLookupComboBox1.DataSource := dsQuicken; // -> ADOQuery -> dbgSearchResults
DBLookupComboBox1.ListSource := dsLookup;
DBLookupComboBox1.DataField := 'ITEM_CODE'; // from DataGrid
DBLookupComboBox1.KeyField := 'ITEM_CODE';
DBLookupComboBox1.ListField := 'ITEM_CODE; DESCRIPTION';
DBLookupComboBox1.Visible := False;
//set ADOQuery1 connection to use the connection string
ADOQuery1.ConnectionString := connString;
//Set SQL query
ADOQuery1.SQL.Text := 'SELECT item_code, description FROM item_code_lookup';
ADOQuery1.Open; //Open ADoquery
//set FieldName display widths
AdoQuery1.FieldByName('ITEM_CODE').DisplayWidth := 30;
AdoQuery1.FieldByName('DESCRIPTION').DisplayWidth := 85;
//set display width for dbLookupCombobox list
dbLookupComboBox1.DropDownWidth := 550;
//set colour for display
dbLookupComboBox1.Color := clInfoBk;
end;
//procdure to draw dbLookupCombobox into the datagrid cell
procedure TfrmMain.dbgSearchResultsDrawColumnCell(Sender: TObject;
const Rect: TRect; DataCol: Integer; Column: TColumn;
State: TGridDrawState);
begin
if (gdFocused in State) then
begin
//check whether datagrid field is the same as the field in the dbLookupComboBox
//combobox
if (Column.Field.FieldName = DBLookupComboBox1.DataField) then
with DBLookupComboBox1 do
begin
//set position in datagrid cell
Left := Rect.Left + dbgSearchResults.Left + 4;
Top := Rect.Top + dbgSearchResults.Top + 40;
Width := Rect.Right - Rect.Left + 5;
Height := Rect.Bottom - Rect.Top;
Visible := True; //show the dbLookupComboBox
end;
end;
end;
//procedure to hide the DBLookupComboBox when datagrid cell looses focus
procedure TfrmMain.dbgSearchResultsColExit(Sender: TObject);
begin
//check whether datagrid field is the same as the field in the dbLookupComboBox
if dbgSearchResults.SelectedField.FieldName = DBLookupComboBox1.DataField then
DBLookupComboBox1.Visible := False //hide the dbLookupComboBox
end;
//prcoedure to show dbLookupComboBox when key is pressed
procedure TfrmMain.dbgSearchResultsKeyPress(Sender: TObject;
var Key: Char);
begin
if (key = Chr(9)) then //tab key is pressed
Exit;
//check whether datagrid field is the same as the field in the dbLookupComboBox
if (dbgSearchResults.SelectedField.FieldName = DBLookupComboBox1.DataField) then
begin
DBLookupComboBox1.SetFocus;
SendMessage(DBLookupComboBox1.Handle, WM_Char, word(Key), 0);
end;
end;
procedure TfrmMain.dbgSearchResultsEnter(Sender: TObject);
begin
//refresh the current display
dbgSearchResults.Refresh;
end;
------------------------------------
There's no place like 127.0.0.1
------------------------------------
I have set it so the DBLookupComboBox is displayed within a cell of the datagrid. When I change a value, the value displayed in the cell shows only the first 10 characters of the item selected in the DBLookupComboBox. Then if I continually use it, the displayed value becomes only the first four characters of the the item selected.
The problem is specifically related to to the DBLookupComboBox, as the datagrid will display the correct values from the datasource, it is only when you change them via the DBLookupComboBox when problems occur. The database fields are set to varchar(50), so I don't see what the problem is.
When I use this piece of code, it does show the selected item in the DBLookupComboBox has been truncated to 4 characters:
//*******************************************************
procedure TfrmMain.DBLookupComboBox1Exit(Sender: TObject);
var
strSelectedItem : String;
begin
strSelectedItem := DBLookupComboBox1.Field.AsString;
showmessage('Selected Item: ' + strSelecteditem);
end;
//*******************************************************
Here is the rest of my code which sets up the DBLookupComboBox:
//set dbLookupCombobox connection and settings
procedure TfrmMain.setDBLookup;
begin
//set data source, connection, and SQL query for the dbLookupCombobox
DBLookupComboBox1.DataSource := dsQuicken; // -> ADOQuery -> dbgSearchResults
DBLookupComboBox1.ListSource := dsLookup;
DBLookupComboBox1.DataField := 'ITEM_CODE'; // from DataGrid
DBLookupComboBox1.KeyField := 'ITEM_CODE';
DBLookupComboBox1.ListField := 'ITEM_CODE; DESCRIPTION';
DBLookupComboBox1.Visible := False;
//set ADOQuery1 connection to use the connection string
ADOQuery1.ConnectionString := connString;
//Set SQL query
ADOQuery1.SQL.Text := 'SELECT item_code, description FROM item_code_lookup';
ADOQuery1.Open; //Open ADoquery
//set FieldName display widths
AdoQuery1.FieldByName('ITEM_CODE').DisplayWidth := 30;
AdoQuery1.FieldByName('DESCRIPTION').DisplayWidth := 85;
//set display width for dbLookupCombobox list
dbLookupComboBox1.DropDownWidth := 550;
//set colour for display
dbLookupComboBox1.Color := clInfoBk;
end;
//procdure to draw dbLookupCombobox into the datagrid cell
procedure TfrmMain.dbgSearchResultsDrawColumnCell(Sender: TObject;
const Rect: TRect; DataCol: Integer; Column: TColumn;
State: TGridDrawState);
begin
if (gdFocused in State) then
begin
//check whether datagrid field is the same as the field in the dbLookupComboBox
//combobox
if (Column.Field.FieldName = DBLookupComboBox1.DataField) then
with DBLookupComboBox1 do
begin
//set position in datagrid cell
Left := Rect.Left + dbgSearchResults.Left + 4;
Top := Rect.Top + dbgSearchResults.Top + 40;
Width := Rect.Right - Rect.Left + 5;
Height := Rect.Bottom - Rect.Top;
Visible := True; //show the dbLookupComboBox
end;
end;
end;
//procedure to hide the DBLookupComboBox when datagrid cell looses focus
procedure TfrmMain.dbgSearchResultsColExit(Sender: TObject);
begin
//check whether datagrid field is the same as the field in the dbLookupComboBox
if dbgSearchResults.SelectedField.FieldName = DBLookupComboBox1.DataField then
DBLookupComboBox1.Visible := False //hide the dbLookupComboBox
end;
//prcoedure to show dbLookupComboBox when key is pressed
procedure TfrmMain.dbgSearchResultsKeyPress(Sender: TObject;
var Key: Char);
begin
if (key = Chr(9)) then //tab key is pressed
Exit;
//check whether datagrid field is the same as the field in the dbLookupComboBox
if (dbgSearchResults.SelectedField.FieldName = DBLookupComboBox1.DataField) then
begin
DBLookupComboBox1.SetFocus;
SendMessage(DBLookupComboBox1.Handle, WM_Char, word(Key), 0);
end;
end;
procedure TfrmMain.dbgSearchResultsEnter(Sender: TObject);
begin
//refresh the current display
dbgSearchResults.Refresh;
end;
------------------------------------
There's no place like 127.0.0.1
------------------------------------