A reference to a ForeignKeyConverter, which allows custom key/value mappings to be provided.
Remarks
By default, foreign key constraints defined by a DataTable or DataView as well as enums can be automatically detected; however, through a ForeignKeyConverter, custom key/value mappings can also be defined. When providing custom key/value mappings, a foreign key converter must be created by deriving from the ForeignKeyConverter class and overriding its GetKeyFromValue and GetValueFromKey methods in which the value for a specified key and the key for a specified value should be returned (see implementation of PersonForeignKeyConverter classin Example 2). This converter can then be provided to the ForeignKeyConverter property of either a foreign key description or configuration.
If a ForeignKeyConverter has not been explicitly provided for a ForeignKeyConfiguration, the converter from its corresponding DataGridForeignKeyDescription will be used.
Example
The following code provides the implementation of the PersonForeignKeyConverter class. The following code provides the implementation of the PersonForeignKeyConverter class.
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
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