第一个问题:这个解决方法很常用,但原方法缩小列的宽度时,DBMemo的宽度不会自动随之缩小
随便放个DBMemo1,设为不可见。
procedure TYanxinForm.DBGrid1DrawDataCell(Sender: TObject;
const Rect: TRect; Field: TField; State: TGridDrawState);
begin
if not DBMemo1.Focused then DBMemo1.Visible :=False; //加了这一句之后,DBMemo的宽度会自动随之缩小,就比较完美了
if (gdFocused in State) or (gdSelected in State) then
if Field.Datatype = ftMemo then
begin
DBMemo1.DataField :=Field.FieldName;
DBMemo1.Left := Rect.Left + DBGrid1.Left;
DBMemo1.Top := Rect.Top + DBGrid1.top;
DBMemo1.Width :=Rect.Right - Rect.Left;
DBMemo1.Height :=DBMemo1.Width;
DBMemo1.Visible := True;
end;
end;
procedure TYanxinForm.DBGrid1ColExit(Sender: TObject);
begin
If DBGrid1.SelectedField.FieldName = DBMemo1.DataField then
DBMemo1.Visible := false;
end;
procedure TYanxinForm.DBGrid1KeyPress(Sender: TObject; var Key: Char);
begin
if (key <> chr(9)) then
begin
if (DBGrid1.SelectedField.FieldName = DBMemo1.DataField) then
begin
DBMemo1.SetFocus;
SendMessage(DBMemo1.Handle, WM_Char, word(Key), 0);
end
else
begin
if Key=Char(VK_RETURN) then
begin
SendMessage(DBGrid1.Handle,WM_KEYDOWN, VK_DOWN,0);
Key:=Char(0);
end;
end;
end;
end;
第二个问题,你可以用循环代码手工添加所有的字段到DBGrid的Column中。
不过这样的话,第一个代码就用不上了,第一段代码可以用的前提条件是DBGrid不可以是被定义过的,
只能是属于那种连上数据源后自动显示的。