Skip to main content

Editing Data

The following page provides a list of examples that demonstrate how to edit data.

tip

All examples in this topic assume that the grid is bound to the Orders table of the Northwind database, unless stated otherwise.

Entering edit when a cell is current

The following example demonstrates how to enter edit mode only when a cell becomes current by setting the EditTriggers property to CellIsCurrent.

<Grid xmlns:xcdg="http://schemas.xceed.com/wpf/xaml/datagrid">
<Grid.Resources>
<xcdg:DataGridCollectionViewSource x:Key="cvs_orders"
Source="{Binding Source={x:Static Application.Current},
Path=Orders}"/>
</Grid.Resources>
<xcdg:DataGridControl x:Name="OrdersGrid"
ItemsSource="{Binding Source={StaticResource cvs_orders}}"
EditTriggers="CellIsCurrent"/>
</Grid>

Using a masked text box

The following example demonstrates how to use the MaskedTextBox control outside a grid to allow a user to input a fictitious identity number. The foreground color of the masked text box will change from red when it contains invalid text (HasParsingError), to blue when all required characters have been inputted (IsMaskCompleted), to green when all characters, required and optional, have been inputted (IsMaskFull).

<Grid xmlns:xcdg="http://schemas.xceed.com/wpf/xaml/datagrid"
xmlns:s="clr-namespace:System;assembly=mscorlib">
<Grid.Resources>
<Style TargetType="{x:Type xcdg:MaskedTextBox}">
<Style.Triggers>
<Trigger Property="HasParsingError" Value="True">
<Setter Property="Foreground" Value="Red" />
</Trigger>
<Trigger Property="IsMaskCompleted" Value="True">
<Setter Property="Foreground" Value="Blue" />
</Trigger>

<Trigger Property="IsMaskFull" Value="True">
<Setter Property="Foreground" Value="Green" />
</Trigger>
</Style.Triggers>
</Style>
</Grid.Resources>
<xcdg:MaskedTextBox Mask=">LLLL 000000 ??"
PromptChar="-"
AllowPromptAsInput="True"
ResetOnPrompt="True"
ResetOnSpace="True"
Height="Auto"/>
</Grid>

Handling routed edit events

The following example demonstrates how to subscribe to the Cell.EditBeginning and EditBegun routed events as well as how to handle and cancel them.

<Grid xmlns:xcdg="http://schemas.xceed.com/wpf/xaml/datagrid">
<Grid.Resources>
<xcdg:DataGridCollectionViewSource x:Key="cvs_orders"
Source="{Binding Source={x:Static Application.Current}, Path=Orders}" />
</Grid.Resources>
<DockPanel>
<StackPanel DockPanel.Dock="Top">
<CheckBox x:Name="handledByRowCheckBox"
Content="Events are handled by the rows"
IsChecked="False" />
<CheckBox x:Name="cancelBeginEdit"
Content="Cancel BeginEdit event"
IsChecked="False" />
</StackPanel>
<xcdg:DataGridControl x:Name="OrdersGrid"
ItemsSource="{Binding Source={StaticResource cvs_orders}}"
xcdg:Cell.EditBeginning="EditBeginning"
xcdg:Cell.EditBegun="EditBegun"/>
</DockPanel>
</Grid>

Defining foreign key configurations

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.

<Grid xmlns:xcdg="http://schemas.xceed.com/wpf/xaml/datagrid">
<Grid.Resources>
<xcdg:DataGridCollectionViewSource x:Key="cvs_orders"
Source="{Binding Source={x:Static Application.Current}, Path=Orders}"
AutoCreateForeignKeyDescriptions="True"/>
</Grid.Resources>

<xcdg:DataGridControl x:Name="OrdersGrid"
ItemsSource="{Binding Source={StaticResource cvs_orders}}"
AutoCreateForeignKeyConfigurations="True">
<xcdg:DataGridControl.Columns>
<xcdg:Column FieldName="EmployeeID"
Title="Employee">
<xcdg:Column.CellContentTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding FirstName}" />
<TextBlock Text=" " />
<TextBlock Text="{Binding LastName}" />
</StackPanel>
</DataTemplate>
</xcdg:Column.CellContentTemplate>
</xcdg:Column>
<xcdg:Column FieldName="CustomerID"
Title="Customer">
<xcdg:Column.ForeignKeyConfiguration>
<xcdg:ForeignKeyConfiguration DisplayMemberPath="CompanyName"
ValuePath="CustomerID" />
</xcdg:Column.ForeignKeyConfiguration>
</xcdg:Column>

<xcdg:Column FieldName="ShipVia"
Title="Shipping Company">
<xcdg:Column.ForeignKeyConfiguration>
<xcdg:ForeignKeyConfiguration DisplayMemberPath="CompanyName"
ValuePath="ShipperID" />
</xcdg:Column.ForeignKeyConfiguration>
</xcdg:Column>
</xcdg:DataGridControl.Columns>
</xcdg:DataGridControl>
</Grid>

Custom key/value mappings

The following example demonstrates how to bind the grid directly to a BindingList<Person> 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.

  <Grid xmlns:xcdg="http://schemas.xceed.com/wpf/xaml/datagrid">
<Grid.Resources>
<local:OccupationToStringConverter x:Key="occupationToStringConverter" />
<local:PersonForeignKeyConverter x:Key="personForeignKeyConverter" />
<ObjectDataProvider x:Key="occupationValues"
MethodName="GetValues"
ObjectType="{x:Type sys:Enum}">
<ObjectDataProvider.MethodParameters>
<x:Type TypeName="local:Occupation" />
</ObjectDataProvider.MethodParameters>
</ObjectDataProvider>
</Grid.Resources>

<xcdg:DataGridControl x:Name="PersonsGrid"
ItemsSource="{Binding Source={x:Static Application.Current}, Path=Persons}">
<xcdg:DataGridControl.Columns>
<xcdg:Column FieldName="Occupation">
<xcdg:Column.CellContentTemplate>
<DataTemplate>
<TextBlock Text="{Binding Converter={StaticResource occupationToStringConverter}}" />
</DataTemplate>
</xcdg:Column.CellContentTemplate>
<xcdg:Column.ForeignKeyConfiguration>
<xcdg:ForeignKeyConfiguration ItemsSource="{Binding Source={StaticResource occupationValues}}" />
</xcdg:Column.ForeignKeyConfiguration>
</xcdg:Column>
<xcdg:Column FieldName="ReportsTo">
<xcdg:Column.CellContentTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding FirstName}" />
<TextBlock Text=" " />
<TextBlock Text="{Binding LastName}" />
</StackPanel>
</DataTemplate>
</xcdg:Column.CellContentTemplate>
<xcdg:Column.ForeignKeyConfiguration>
<xcdg:ForeignKeyConfiguration ItemsSource="{Binding Source={x:Static Application.Current}, Path=Persons}"
ForeignKeyConverter="{StaticResource personForeignKeyConverter}"
ValuePath="PersonID"/>
</xcdg:Column.ForeignKeyConfiguration>
</xcdg:Column>
</xcdg:DataGridControl.Columns>
</xcdg:DataGridControl>
</Grid>

The following code provides the implementation of the OccupationToStringConverter class.

public class OccupationToStringConverter: IValueConverter
{
public object Convert( object value, Type targetType, object parameter, System.Globalization.CultureInfo culture )
{
if( value != null && value is Occupation)
{
string enumString = value.ToString();
// Start at 1 to ignore the first capitalizes letter.
for( int i = 1; i < enumString.Length - 1; i++ )
{
if( char.IsUpper( enumString[ i ] ) )
{
enumString = enumString.Insert( i, " " );
i++;
}
}
return enumString;
}
return null;
}
public object ConvertBack( object value, Type targetType, object parameter, System.Globalization.CultureInfo culture )
{
return Binding.DoNothing;
}
}