Skip to main content
A newer version of this page is available. .

Lesson 2 - Configure Columns and Editors

  • 3 minutes to read

This tutorial demonstrates how to adjust the column layout, specify cell editors, and format displayed values. The tutorial is based on Lesson 1.

GridControl Tutorial Result

Change Column Layout

Fit columns to the GridControl and set the optimal width for all columns to display their contents completely:

GridControl Tutorial AutoWidth

  1. Set the TableView.AutoWidth property to true to fit columns to the GridControl.
  2. Set the TableView.BestFitModeOnSourceChange property to VisibleRows to calculate the optimal width for all columns:

    <dxg:GridControl>
        <dxg:GridControl.View>
            <dxg:TableView AutoWidth="True" 
                           BestFitModeOnSourceChange="VisibleRows"/>
        </dxg:GridControl.View>
        <!-- ... -->
    </dxg:GridControl>
    

Specify an In-Place Editor

The GridControl uses in-place editors to edit cell values. The editor’s type 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 also define a custom editor as follows (for example, the ComboBoxEdit):

GridControl Tutorial ComboboxEdit

Add a Shippers collection to the ViewModel:

using DevExpress.Mvvm;
using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
using WPFBlankDotNETCoreAppWithNorthwindDatabase.Models;

namespace WPFBlankDotNETCoreAppWithNorthwindDatabase {
    public class ViewModel : ViewModelBase {
        public ObservableCollection<Shipper> Shippers {
            get => GetValue<ObservableCollection<Shipper>>();
            private set => SetValue(value);
        }
        public IList<Order> ItemsSource {
            get {
                if (_ItemsSource == null && !DevExpress.Mvvm.ViewModelBase.IsInDesignMode) {
                    _Context = new NorthwindEntities();
                    _ItemsSource = _Context.Orders.ToList();
                }
                _Context.Shippers.Load();
                Shippers = _Context.Shippers.Local.ToObservableCollection();
                return _ItemsSource;
            }
        }
        //...
    }
}

In Designer

  1. Build the solution to make the Shippers collection visible in the Visual Studio XAML Designer.

  2. Invoke the Feature Browser for the GridControl.

    WPF Run FeatureBrowser

  3. Locate the Data Editing and Validation category and select Inplace Data Editing. Select the ShipVia column and set the EditSettings property to ComboBoxEditSettings:

    GridControl Tutorial EditSettings

  4. Set the Shippers collection as the ComboBoxEdit‘s ItemsSource:

    GridControl Tutorial ComboboxEdit ItemsSource

  5. Set the DisplayMember property to CompanyName and the ValueMember property to ShipperID:

    GridControl Tutorial DisplayMember

In XAML

  1. Add the xmlns:dxe="http://schemas.devexpress.com/winfx/2008/xaml/editors" namespace to the ThemedWindow.

  2. Create the ComboBoxEditSettings object and assign it to the ShipVia column’s ColumnBase.EditSettings property.

  3. Specify the ItemsSource property to bind the ComboBoxEdit to data.

  4. Set the DisplayMember property to CompanyName and the ValueMember property to ShipperID:

    <dx:ThemedWindow
        ...
        xmlns:dxe="http://schemas.devexpress.com/winfx/2008/xaml/editors">
        <!-- ... -->
        <dxg:GridColumn FieldName="ShipVia" IsSmart="True">
            <dxg:GridColumn.EditSettings>
                <dxe:ComboBoxEditSettings ItemsSource="{Binding Shippers}" 
                                          DisplayMember="CompanyName" 
                                          ValueMember="ShipperID"/>
            </dxg:GridColumn.EditSettings>
        </dxg:GridColumn>
    

Refer to the following topic for details about in-place editors: Assign Editors to Cells.

Format Values

You can configure how the GridControl displays data. The following example formats the Freight column’s data as currency:

GridControl Tutorial Mask

In Designer

  1. Invoke the Freight column’s Quick Actions and select Create EditSettings:

    GridControl Tutorial Create EditSettings

  2. Set the EditSettings property to TextEditSettings:

    GridControl Tutorial TextEditSettings

  3. Open the Mask Editor window:

    GridControl Tutorial Open Mask Editor

  4. Select the Numeric mask type and choose the Currency mask.

  5. Check Use mask as DisplayFormat and click OK:

    GridControl Tutorial Mask Settings

In XAML

  1. Create a TextEditSettings object and assign it to the Freight column’s ColumnBase.EditSettings property.

  2. Specify the TextEditSettings.MaskType and TextEditSettings.Mask properties to set the in-placed editor’s mask to Currency.

  3. Set the TextEditSettings.MaskUseAsDisplayFormat property to true:

    <dxg:GridColumn FieldName="Freight" IsSmart="True">
        <dxg:GridColumn.EditSettings>
            <dxe:TextEditSettings Mask="c" MaskType="Numeric"            
                                  MaskUseAsDisplayFormat="True"/>
        </dxg:GridColumn.EditSettings>
    </dxg:GridColumn>
    

Refer to the following topic for details about formatting cell values: Format Cell Values.

See Also