This sample demonstrates the RelationKind.ForeignKeyReference relation.
Features:
The foreign-key reference concept is implemented with two class files: USStates.cs and Countries.cs.
USStates.cs contains two classes: USState and USStatesCollection.
The class USState has two properties: Key and Name.
The USStatesCollection class is implemented from the array list.
It has a method called CreateDefaultCollection() and creates a collection of USStates.
Countries.cs has two classes: Country and CountriesCollection.
The class Country has two properties: CountryCode and Name.
The Country collection is implemented from the array list.
The following code establishes the foreign-key relation.
// Creating a Relation Descriptor. GridRelationDescriptor usStatesRd = new GridRelationDescriptor(); usStatesRd.Name = "State"; // Initialize it's RelationKind. usStatesRd.RelationKind = RelationKind.ForeignKeyReference; // Initialize it's ChildTable from the look-up name of SourceLIstSet. usStatesRd.ChildTableName = "USStates"; // SourceListSet name for lookup // Mention the file that establishes the relation. usStatesRd.RelationKeys.Add("State", "Key"); // Making the relation as 'ReadOnly'. usStatesRd.ChildTableDescriptor.AllowEdit = false; usStatesRd.ChildTableDescriptor.AllowNew = false; // Adding the relation descriptor to the MainTableDescriptor. mainTd.Relations.Add(usStatesRd);
Foreign Key Helper
This sample also provides an option to use ForeignKeyHelper, a built-in helper class that can easily set up a ForeginKeyReference relation with a single method call.
GridForeignKeyHelper.SetupForeignTableLookUp(gridGroupingControl1, "Country", countries, "CountryCode", "CountryName");