tags:

views:

157

answers:

2

I have a column in my dbgrid that is based on a lookup field.

The problem is that the end user can't set a blank value for the field - they can only select values from the lookup table.

How can I allow the end user to delete or 'blank out' a value for the column?

A: 

Try putting a blank value in the lookup table.

Mason Wheeler
I thought of that - surely there's a way to do this without adding extra data...?
croceldon
Not that I know of. The point of a lookup is to give you choices based on a list defined somewhere else. If you start adding options that aren't on that list then you no longer have a lookup.
Mason Wheeler
+2  A: 

In the appropriate TDBGrid Key event, trap for DEL. When detected, check to see if you're in the lookup column. If so, call Clear on corresponding dataset field.

Larry Lustig
How can I tell if I'm in the lookup column?
croceldon
Read DBGrid.SelectedIndex or, even better, DBGrid.SelectedField which will give you the field object to Clear() directly.
Larry Lustig
Well, that's really close. I'm calling something like grid.SelectedField.Clear, but it doesn't seem to work. The field is unaffected in the grid.
croceldon
Ok, here's what I came up with that works: with grid.DataSource.Dataset do begin Edit; FieldByName('fieldname').Clear; Post; end;Probably a better way of doing it, but that works. Thanks!
croceldon
Glad I was able to get you close, even if not all the way. Not sure I've done this in a grid before, but I have with DBComboBox set to DropDownList style — same idea, but easier to get back to the appropriate field object.
Larry Lustig