|
1 | | -# How to customize the edit mode behavior of the cells in the wpf datagrid? |
2 | | -This example demonstrates to customize the edit mode behavior of the cells in the wpf datagrid |
| 1 | +# How to Customize Edit Mode Behavior of GridCell in WPF DataGrid? |
| 2 | + |
| 3 | +This example demonstrates how to customize edit mode behavior of GridCell in [WPF DataGrid](https://www.syncfusion.com/wpf-controls/datagrid) (SfDataGrid). |
| 4 | + |
| 5 | +In **DataGrid**, you can directly go to the edit mode in **GridCell** by pressing any letter or digit as an input from the keyboard. |
| 6 | + |
| 7 | +By default, DataGrid does not allow the GridCell to go to edit mode while pressing the Minus (-) key or any special character. You can overcome this behavior by customizing the `SfDataGrid` class, and overriding its `OnTextInput` method. |
| 8 | + |
| 9 | +**Note:** |
| 10 | +By default, **GridTemplateColumn** goes to edit mode while pressing the F2 key when you load the EditTemplate for it. No other letters or digits allow edit mode for GridTemplateColumn, GridCheckBoxColumn, GridImageColumn, GridHyperlinkColumn and GridUnboundColumn. |
| 11 | + |
| 12 | +The following code example illustrates how to override the `SfDataGrid` class and customize the editing behavior in `OnTextInput` method. |
| 13 | + |
| 14 | +#### C# |
| 15 | + |
| 16 | +``` csharp |
| 17 | +public class SfDataGridExt : SfDataGrid |
| 18 | +{ |
| 19 | + public SfDataGridExt() : base() |
| 20 | + { |
| 21 | + } |
| 22 | + |
| 23 | + protected override void OnTextInput(TextCompositionEventArgs e) |
| 24 | + { |
| 25 | + if (!SelectionController.CurrentCellManager.HasCurrentCell) |
| 26 | + { |
| 27 | + base.OnTextInput(e); |
| 28 | + return; |
| 29 | + } |
| 30 | + |
| 31 | + var rowColumnIndex = SelectionController.CurrentCellManager.CurrentRowColumnIndex; |
| 32 | + var row = this.ResolveToRowIndex(rowColumnIndex.RowIndex); |
| 33 | + var dataRow = this.RowGenerator.Items.FirstOrDefault(item => item.RowIndex == rowColumnIndex.RowIndex); |
| 34 | + |
| 35 | + if (dataRow != null && dataRow is DataRow) |
| 36 | + { |
| 37 | + var dataColumn = dataRow.VisibleColumns.FirstOrDefault(column => column.ColumnIndex == rowColumnIndex.ColumnIndex); |
| 38 | + char text; |
| 39 | + char.TryParse(e.Text, out text); |
| 40 | + if (dataColumn != null && !(dataColumn.GridColumn is GridTemplateColumn) && !dataColumn.IsEditing && SelectionController.CurrentCellManager.BeginEdit()) |
| 41 | + dataColumn.Renderer.PreviewTextInput(e); |
| 42 | + } |
| 43 | + base.OnTextInput(e); |
| 44 | + } |
| 45 | +} |
| 46 | +``` |
| 47 | + |
| 48 | +In the above code example, the `OnTextInput` method is fired when the input is received from the keyboard for Grid Column's cell. With the help of **CurrentRowColumnIndex** in **CurrentCellManager** of **SelectionController**, you can get the current column from VisibleColumns collection that receives the input from keyboard. |
| 49 | + |
| 50 | +The editing is skipped when the current column is **GridTemplateColumn** and it is already in edit mode. With the existing condition, you need to include the condition to allow negative number also. |
| 51 | + |
| 52 | +The above customized **SfDataGrid** should be loaded in XAML for implementing the editing behavior in GridCell. Refer the following code example. |
| 53 | + |
| 54 | +#### XAML |
| 55 | + |
| 56 | +``` xml |
| 57 | +<local:SfDataGridExt x:Name="grid" |
| 58 | + ItemsSource="{Binding Students}" |
| 59 | + AllowResizingColumns="True" |
| 60 | + AllowResizingHiddenColumns="False" |
| 61 | + AllowDraggingColumns="True" |
| 62 | + GridValidationMode="InView" |
| 63 | + AutoGenerateColumns="False" |
| 64 | + AllowEditing="True"> |
| 65 | + <local:SfDataGridExt.Columns> |
| 66 | + <syncfusion:GridTextColumn HeaderText="Id" MappingName="Id" /> |
| 67 | + <syncfusion:GridTextColumn HeaderText="Name" MappingName="Name" TextWrapping="Wrap" /> |
| 68 | + <syncfusion:GridNumericColumn HeaderText="Salary" MappingName="Salary" /> |
| 69 | + <syncfusion:GridNumericColumn HeaderText="GradePoint Average" MappingName="GradePointAverage" /> |
| 70 | + </local:SfDataGridExt.Columns> |
| 71 | +</local:SfDataGridExt > |
| 72 | +``` |
0 commit comments