Nothing is more boring than typing the same thing over and over again. Especially, if there are only a few exceptions to the rule. Yet again, I built up my sample data from the LEGO Dimensions world.
You do not need to know what LEGO Dimensions is to understand the example. If you are interested … Google is your friend 🙂
Basically, we have several packs of LEGO bricks. Every pack is one of the following types:
- Level Pack
- Team Pack
- Fun Pack
- Story Pack
The packs always contain content from a certain “world”, I refer to them as “Themes”.
As people would not like referring to these packs by their item number, Lego also assigned titles to these packs. E.g. “Ghostbusters Level Pack” or “DC Comics Team Pack”. I guess, you already know where I am going with this.
When entering the pack data using a grid, I set up the grid in a way that I enter its number first, then the type and finally the theme. The title of every pack is mostly derived from these values:
The grid will suggest…
….which I only need to confirm as correct. Another rule is that there is no common name for Fun Packs, thus, the grid should not be filled with text that needs to be deleted anyways:
So, how tedious is this to implement? It is rather easy as the grid offers an event that allows you to set the default value when starting to edit a cell. When finishing the edit, there is yet another event that will allow you to transform the values entered by the user:
- GetEditText is the event to prepare a default text for the user
- SetEditText can be used to transform user input before it is passed on to the grid
Here is the code for the GetEditText event (yes, I prefer my source code in 14 pts — will adjust the font the next time):
procedure TFrmPacks.gridPacksGetEditText(Sender: TObject; ACol, ARow: Integer; var Value: string); var lTitle : String; lPackTypeName: String; lThemeName: String; begin // -- title column if gridPacks.Columns[ACol].FieldName = 'Title' then begin // get current value lTitle := gridPacks.Cells[ACol, ARow]; // only suggest a value if there is no content if ( lTitle = String.Empty ) then begin lPackTypeName := gridPacks.Cells[ gridPacks.ColumnByFieldName['PackTypeName'].Index, ARow ]; // ignore Fun Packs if lPackTypeName <> 'Fun' then begin lThemeName := gridPacks.Cells[ gridPacks.ColumnByFieldName['ThemeName'].Index, ARow ]; // hand value to grid Value := lThemeName + ' ' + lPackTypeName + ' Pack'; end; end; end; end;
Pretty easy if you know which events to pick … you do not even have to touch any database related topics if you are using a TDBAdvGrid. The datasource is being fed by the grid … not your job!