Lesson 2 - Display and Edit Data
- 2 minutes to read
This tutorial demonstrates how to display and edit data within the GridControl. This tutorial is based on the Lesson 1.
Operate a Set of Columns
The GridControl generates columns for all fields from a bound data source. You can add columns to the GridControl explicitly. Define columns and use the GridControl.Columns property to add them to the GridControl:
<dxg:GridControl AutoGenerateColumns="AddNew" ItemsSource="{Binding Customers}">
<dxg:GridControl.Columns>
<dxg:GridColumn FieldName="Name" IsSmart="True"/>
<dxg:GridColumn FieldName="City" IsSmart="True"/>
<dxg:GridColumn FieldName="Visits" IsSmart="True"/>
<dxg:GridColumn FieldName="Birthday" IsSmart="True"/>
</dxg:GridControl.Columns>
<dxg:GridControl.View>
<dxg:TableView />
</dxg:GridControl.View>
</dxg:GridControl>
Change an In-Place Editor
The GridControl uses an in-place editor that depends on a column’s content. The CheckEdit is used for boolean values, the DateEdit - for dates, the TextEdit - for strings and numbers. You can define a custom editor (for example, the ComboBoxEdit):
Create a list of cities in the View Model:
namespace DxGridGettingStarted { public class MainWindowViewModel : INotifyPropertyChanged { public MainWindowViewModel() { // ... var cities = from c in people select c.City; Cities = cities.Distinct().ToList(); } public List<Customer> Customers { get; private set; } // A list of cities for ComboBoxEdit public List<string> Cities { get; private set; } public event PropertyChangedEventHandler PropertyChanged; } }
Add the
xmlns:dxe="http://schemas.devexpress.com/winfx/2008/xaml/editors"
namespace to the Window.Create the ComboBoxEditSettings object and assign it to the City column’s ColumnBase.EditSettings property.
Specify the ComboBoxEditSettings.ItemsSource property to bind ComboBoxEdit to data:
<Window ... xmlns:dxe="http://schemas.devexpress.com/winfx/2008/xaml/editors" ... > <dxg:GridColumn FieldName="City" IsSmart="True"> <dxg:GridColumn.EditSettings> <dxe:ComboBoxEditSettings ItemsSource="{Binding Cities}"/> </dxg:GridColumn.EditSettings> </dxg:GridColumn>
Format Values
You can configure how the GridControl displays data. The following example formats the Birthday column’s data:
- Create the DateEditSettings object and assign it to the Birthday column’s ColumnBase.EditSettings property.
- Set an in-placed editor’s mask to Month day with the TextEditSettings.Mask property.
Set the TextEditSettings.MaskUseAsDisplayFormat property to true:
<dxg:GridColumn FieldName="Birthday" IsSmart="True"> <dxg:GridColumn.EditSettings> <dxe:DateEditSettings Mask="m" MaskUseAsDisplayFormat="True"/> </dxg:GridColumn.EditSettings> </dxg:GridColumn>