A reference to a ForeignKeyConfiguration through which the visual representation of a foreign key description can be configured.
Remarks
By default, if a CellContentTemplate is specified, the same template will be applied to the foreign key configuration (see EmployeeID column in Example 1). For simpler configurations, or when a cell-content template is not required, a configuration's ValuePath and DisplayMemberPath can be used to indicate the path to the value on the source object that corresponds to the "key" and the path to its its visual representation, respectively (see ShipVia and CustomerID columns in Example).
Example
All examples in this topic assume that the grid is bound to the Orders table of the Northwind database or a collection of Person objects, unless stated otherwise.
The following example demonstrates how to define foreign key configurations for foreign key descriptions that were automatically created from the constraints extracted from the underlying DataTable.
The following example demonstrates how to bind the grid directly to a BindingList objects and provide a custom key/value mapping through a ForeignKeyConverter, which will return the appropriate employee first and last names for the provided employee ID.
PublicClass PersonForeignKeyConverter
Inherits ForeignKeyConverter
PublicOverridesFunction GetKeyFromValue( value AsObject, configuration As ForeignKeyConfiguration ) AsObjectDim bindingList As PersonBindingList = TryCast( configuration.ItemsSource, PersonBindingList )
IfNot bindingList IsNothingThenDim person As Person = TryCast( value, Person )
IfNot person IsNothingThenReturn person.PersonID
EndIfEndIfReturn -1
End FunctionPublicOverridesFunction GetValueFromKey( key AsObject, configuration As ForeignKeyConfiguration ) AsObjectDim bindingList As PersonBindingList = TryCast( configuration.ItemsSource, PersonBindingList )
IfNot bindingList IsNothingThenTryDim personID AsInteger = CInt( key )
Dim person As Person
ForEach person In bindingList
If person.PersonID = personID ThenReturn person
EndIfNext person
Catch e As Exception
' key can be nothing
EndTryReturnNothingEnd FunctionEnd Class
The following code provides the implementation of the PersonForeignKeyConverter class.
publicclass PersonForeignKeyConverter : ForeignKeyConverter
{
publicoverrideobject GetKeyFromValue( object value, ForeignKeyConfiguration configuration )
{
PersonBindingList bindingList = configuration.ItemsSource as PersonBindingList;
if( bindingList != null )
{
Person person = value as Person;
if( person != null )
{
return person.PersonID;
}
}
return -1;
}
publicoverrideobject GetValueFromKey( object key, ForeignKeyConfiguration configuration )
{
PersonBindingList bindingList = configuration.ItemsSource as PersonBindingList;
if( bindingList != null )
{
try
{
int personID = ( int )key;
foreach( Person person in bindingList )
{
if( person.PersonID == personID )
{
return person;
}
}
}
catch( Exception )
{
// key can be null
}
}
returnnull;
}
}
Requirements
Target Platforms: Windows 11, Windows 10, Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2