|
5 | 5 | [](#does-this-example-address-your-development-requirementsobjectives)
|
6 | 6 | <!-- default badges end -->
|
7 | 7 |
|
8 |
| -# WPF Data Grid - Display custom text in cells based on a specific condition |
| 8 | +# WPF Data Grid – Display Custom Text in Cells Based on a Condition |
9 | 9 |
|
10 |
| -This example shows how to display custom text in data cells. In this example, the [GridControl](https://docs.devexpress.com/WPF/DevExpress.Xpf.Grid.GridControl) adds the `(SALE)` string to the **Product Name** if a value in the **Discount** column is greater than 20. |
| 10 | +In this example, the [`GridControl`](https://docs.devexpress.com/WPF/DevExpress.Xpf.Grid.GridControl) changes cell display text when a specific condition is met. |
11 | 11 |
|
12 |
| - |
| 12 | +If the **Discount** value is greater than 20, the grid appends **(SALE)** to the **Product Name**. |
13 | 13 |
|
14 |
| -<!-- default file list --> |
| 14 | + |
15 | 15 |
|
16 |
| -## Files to Look At |
| 16 | +Use this technique when you need to: |
17 | 17 |
|
18 |
| -### Code Behind Technique |
| 18 | +- Add labels or status markers to cell values. |
| 19 | +- Update cell text dynamically while the original data remains unchanged. |
| 20 | +- Improve readability and highlight key values. |
19 | 21 |
|
20 |
| -- [MainWindow.xaml](./CS/DisplayCustomText_CodeBehind/MainWindow.xaml) ([MainWindow.xaml](./VB/DisplayCustomText_CodeBehind/MainWindow.xaml)) |
21 |
| -- [MainWindow.xaml.cs](./CS/DisplayCustomText_CodeBehind/MainWindow.xaml.cs#L20-L25) ([MainWindow.xaml.vb](./VB/DisplayCustomText_CodeBehind/MainWindow.xaml.vb#L22-L29)) |
| 22 | +## Implementation Details |
22 | 23 |
|
23 |
| -### MVVM Technique |
| 24 | +The example includes [code-behind](#code-behind) and [MVVM](#mvvm) techniques. |
24 | 25 |
|
25 |
| -- [MainWindow.xaml](./CS/DisplayCustomText_MVVM/MainWindow.xaml) ([MainWindow.xaml](./VB/DisplayCustomText_MVVM/MainWindow.xaml)) |
26 |
| -- [MainViewModel.cs](./CS/DisplayCustomText_MVVM/MainViewModel.cs#L32-L40) ([MainViewModel.vb](./VB/DisplayCustomText_MVVM/MainViewModel.vb#L76-L84)) |
| 26 | +### Code-Behind |
27 | 27 |
|
28 |
| -<!-- default file list end --> |
| 28 | +Handle the [`CustomColumnDisplayText`](https://docs.devexpress.com/WPF/DevExpress.Xpf.Grid.GridControl.CustomColumnDisplayText) event to replace or extend the text displayed in a cell based on custom conditions. |
| 29 | + |
| 30 | +```csharp |
| 31 | +void grid_CustomColumnDisplayText(object sender, CustomColumnDisplayTextEventArgs e) { |
| 32 | + if (e.Column.FieldName == "ProductName") { |
| 33 | + decimal discount = (decimal)e.GetCellValue("Discount"); |
| 34 | + if (discount > 20) |
| 35 | + e.DisplayText += " (SALE)"; |
| 36 | + } |
| 37 | +} |
| 38 | +``` |
| 39 | + |
| 40 | +### MVVM |
| 41 | + |
| 42 | +Bind the [CustomColumnDisplayTextCommand](https://docs.devexpress.com/WPF/DevExpress.Xpf.Grid.GridControl.CustomColumnDisplayTextCommand) to a view model command to change the text displayed in a cell based on custom conditions. |
| 43 | + |
| 44 | +```csharp |
| 45 | +public void OnCustomColumnDisplayText(CustomColumnDisplayTextEventArgs e) { |
| 46 | + if (e.Column.FieldName == "ProductName") { |
| 47 | + decimal discount = (decimal)e.GetCellValue("Discount"); |
| 48 | + if (discount > 20) |
| 49 | + e.DisplayText += " (SALE)"; |
| 50 | + } |
| 51 | +} |
| 52 | +``` |
| 53 | + |
| 54 | +## Files to Review |
| 55 | + |
| 56 | +### Code-Behind |
| 57 | + |
| 58 | +* [MainWindow.xaml](./CS/DisplayCustomText_CodeBehind/MainWindow.xaml) (VB: [MainWindow.xaml](./VB/DisplayCustomText_CodeBehind/MainWindow.xaml)) |
| 59 | +* [MainWindow.xaml.cs](./CS/DisplayCustomText_CodeBehind/MainWindow.xaml.cs#L20-L25) (VB: [MainWindow.xaml.vb](./VB/DisplayCustomText_CodeBehind/MainWindow.xaml.vb#L22-L29)) |
| 60 | + |
| 61 | +### MVVM |
| 62 | + |
| 63 | +* [MainWindow.xaml](./CS/DisplayCustomText_MVVM/MainWindow.xaml) (VB: [MainWindow.xaml](./VB/DisplayCustomText_MVVM/MainWindow.xaml)) |
| 64 | +* [MainViewModel.cs](./CS/DisplayCustomText_MVVM/MainViewModel.cs#L32-L40) (VB: [MainViewModel.vb](./VB/DisplayCustomText_MVVM/MainViewModel.vb#L76-L84)) |
29 | 65 |
|
30 | 66 | ## Documentation
|
31 | 67 |
|
32 |
| -- [Format Cell Values](https://docs.devexpress.com/WPF/400449/controls-and-libraries/data-grid/appearance-customization/format-cell-values) |
33 |
| -- [GridControl.CustomColumnDisplayText](https://docs.devexpress.com/WPF/DevExpress.Xpf.Grid.GridControl.CustomColumnDisplayText) |
34 |
| -- [TreeListView.CustomColumnDisplayText](https://docs.devexpress.com/WPF/DevExpress.Xpf.Grid.TreeListView.CustomColumnDisplayText) |
35 |
| -- [GridControl.CustomColumnDisplayTextCommand](https://docs.devexpress.com/WPF/DevExpress.Xpf.Grid.GridControl.CustomColumnDisplayTextCommand) |
36 |
| -- [TreeListView.CustomColumnDisplayTextCommand](https://docs.devexpress.com/WPF/DevExpress.Xpf.Grid.TreeListView.CustomColumnDisplayTextCommand) |
| 68 | +* [GridControl](https://docs.devexpress.com/WPF/DevExpress.Xpf.Grid.GridControl) |
| 69 | +* [Format Cell Values](https://docs.devexpress.com/WPF/400449/controls-and-libraries/data-grid/appearance-customization/format-cell-values) |
| 70 | +* [GridControl.CustomColumnDisplayText](https://docs.devexpress.com/WPF/DevExpress.Xpf.Grid.GridControl.CustomColumnDisplayText) |
| 71 | +* [GridControl.CustomColumnDisplayTextCommand](https://docs.devexpress.com/WPF/DevExpress.Xpf.Grid.GridControl.CustomColumnDisplayTextCommand) |
37 | 72 |
|
38 | 73 | ## More Examples
|
39 | 74 |
|
40 |
| -- [How to Display custom text within data cells and groups](https://github.com/DevExpress-Examples/how-to-display-custom-text-within-data-cells-and-groups-t327301) |
41 |
| -- [How to Apply Custom Rules to Group Rows](https://github.com/DevExpress-Examples/how-to-implement-custom-grouping-e1530) |
| 75 | +* [Display Custom Text Within Data Cells and Groups](https://github.com/DevExpress-Examples/how-to-display-custom-text-within-data-cells-and-groups-t327301) |
| 76 | +* [Apply Custom Rules to Group Rows](https://github.com/DevExpress-Examples/how-to-implement-custom-grouping-e1530) |
| 77 | + |
42 | 78 | <!-- feedback -->
|
43 | 79 | ## Does this example address your development requirements/objectives?
|
44 | 80 |
|
|
0 commit comments