Design-Time Features > Automatic Lookup Columns in Grids |
A common scenario in data binding is for data classes to contain references to other data classes. For example, a Product object may contain references to Category and Supplier objects.
In ADO.NET, the references usually appear as foreign keys that map to other tables (e.g., Product.CategoryID and Product.SupplierID).
In the Entity Framework, you still get the key columns, but you also get the actual objects. So you have Product.CategoryID (usually an integer) and Product.Category (an actual Category object).
Displaying foreign keys in a grid is not very useful, because it is unlikely that users will remember that category 12 is "Dairy Products" or that supplier 15 is "ACME Imports". Allowing users to edit these keys would be even worse. A common way to work around this problem is to remove the related entity columns from any bound grids and, optionally, to replace them with custom columns that use combo boxes for editing the values, so called lookups. The combo boxes have to be bound to the related tables and have their properties set so they display relevant values (e.g., Category.Name or Supplier.CompanyName) and so they are synchronized with the values being displayed on the grid. This is not terribly hard to do, but it is a tedious and error-prone task that makes projects harder to create and maintain.
C1DataSource can do this tedious work for the developer; it can automatically change related entity columns so that they show combo box lookups. It can do this for several types of data grids, those that it supports. Currently supported WinForms grids are: C1FlexGrid and Microsoft DataGridView. Here we will show how to do this for C1FlexGrid.
C1DataSource provides an extender property called ControlHandler. If you place a C1FlexGrid control on a form that contains a C1DataSource, the grid will get an additional ControlHandler property. A ControlHandler is an object containing (at present) a single boolean property AutoLookup. This property set to True causes the C1DataSource to configure grid columns that contain references to other entities so that they show lookup combos.
To see how it works, follow these steps:
As you can see, the Category and Supplier columns are not useful at all. You could remove them or customize the grid by writing some code to create new columns, but there’s an easier way.
Remember, this is an extender property and will be available only if there is a C1DataSource component on the form. Set the AutoLookup property there to True.
The string value (name) shown by the combo box is determined following these rules: