对应的编辑控件为ComboBoxEx1
procedure TForm13.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect; DataCol: Integer; Column: TColumn; State: TGridDrawState); var Im1: TBitmap; begin if (gdFocused in State) then begin //====================ComboBoxEx1 coloer======================== if (Column.Field.FieldName = 'aBitmap') then with ComboBoxEx1 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; ComboBoxEx1.ItemIndex := ComboBoxEx1.Items.IndexOf(Column.Field.Value); // 从 数据库 中读 并 设置 ComboBoxEx1 Visible := True; end; end; //====================================================================== if (Column.Field.FieldName = 'aBitmap') then begin Im1 := TBitmap.Create; with DBGrid1.Canvas do begin Brush.Color := clinfoBk; FillRect(Rect); if (Column.Field.AsInteger = 0) then ImageList1.GetBitmap(0, Im1); if (Column.Field.AsInteger = 1) then ImageList1.GetBitmap(1, Im1); if (Column.Field.AsInteger = 2) then ImageList1.GetBitmap(2, Im1); Draw(round((Rect.Left + Rect.Right - Im1.Width) / 2), Rect.Top, Im1); end; ComboBoxEx1.ItemIndex := ComboBoxEx1.Items.IndexOf(Column.Field.Value); end; //===================================================================== end;
procedure TForm13.DBGrid1ColExit(Sender: TObject); // 退出 时,不显示 编辑 控件 begin if DBGrid1.SelectedField.FieldName = 'aBitmap' then ComboBoxEx1.Visible := False; end;
procedure TForm13.ComboBoxEx1Change(Sender: TObject); begin //将修改值写回数据库里 DBGrid1.DataSource.Edit; DBGrid1.Columns[5].Field.asinteger:= ComboBoxEx1.ItemIndex; //5 为 'aBitmap'ComboBoxEx1所在的列序号 FDQuery1.Post; end;