DBGrid中插入ComboBox1(10)

发布时间 2024-01-05 11:52:12作者: 涂磊

根据  DBGrid中插入DateTimePicker(08)  类似 改编

 

procedure TForm13.ComboBox1Change(Sender: TObject);   //ComboBox1  写回  数据库
begin
    DBGrid1.DataSource.Edit;
    DBGrid1.Columns[1].Field.Value  := ComboBox1.Items[ComboBox1.ItemIndex ];    //1 为 'username'所在的列序号
    fdquery1.Post;
end;


procedure TForm13.DBGrid1ColExit(Sender: TObject);
begin
  if DBGrid1.SelectedField.FieldName = 'username' then
    ComboBox1.Visible := False
end;

procedure TForm13.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
  DataCol: Integer; Column: TColumn; State: TGridDrawState);
begin
  if (gdFocused in State) then
  begin
    if (Column.Field.FieldName = 'username') then
    with ComboBox1 do
    begin
      Left := Rect.Left + DBGrid1.Left + 1;
      Top := Rect.Top + DBGrid1.Top + 1;
      Width := Rect.Right - Rect.Left + 2;
      Width := Rect.Right - Rect.Left + 2;
      Height := Rect.Bottom - Rect.Top + 2;

     ComboBox1.ItemIndex  := ComboBox1.Items.IndexOf( Column.Field.Value);  //  从 数据库 中读 并 设置   ComboBox1
      Visible := True;
    end;
  end
end;

procedure TForm13.FDQuery1usernameGetText(Sender: TField; var Text: string;    //Sqlite wideMemo显示 字符
  DisplayText: Boolean);
begin
Text := Copy(FDQuery1UserName.AsString, 1, 50);
end;