wpf-109751-controls-and-libraries-data-grid-getting-started-code-lesson-2-display-and-edit-data.md
This tutorial demonstrates how to adjust the column layout, specify cell editors, and format displayed values. The tutorial is based on Lesson 1.
The GridControl generates columns for all fields in a bound data source if the AutoGenerateColumns property is set to AddNew. Add columns to the GridControl explicitly to display only required columns and access settings for each column:
None (default value).<dxg:GridControl EnableSmartColumnsGeneration="True"
ItemsSource="{Binding Orders}">
<dxg:GridControl.View>
<dxg:TableView/>
</dxg:GridControl.View>
<dxg:GridColumn FieldName="OrderId"/>
<dxg:GridColumn FieldName="CustomerId"/>
<dxg:GridColumn FieldName="OrderDate"/>
<dxg:GridColumn FieldName="ShipVia"/>
<dxg:GridColumn FieldName="Freight"/>
<dxg:GridColumn FieldName="ShipName"/>
<dxg:GridColumn FieldName="ShipCity"/>
<dxg:GridColumn FieldName="ShipCountry"/>
</dxg:GridControl>
Refer to the following help topic for more information: Create Columns and Bind Them to Data Properties.
Fit columns to the GridControl and set the optimal width for all columns to display their contents completely:
true to fit columns to the GridControl.<dxg:GridControl.View>
<dxg:TableView AutoWidth="True"
BestFitModeOnSourceChange="VisibleRows"/>
</dxg:GridControl.View>
Refer to the following help topic for more information: Move and Resize Columns.
The GridControl uses in-place editors to edit cell values. The editor type depends on column content. The CheckEdit is used for Boolean values, the DateEdit – for dates, and the TextEdit – for strings and numbers. You can also define a custom editor as follows (for example, the ComboBoxEdit):
Add a Shippers collection to the View Model:
Add the xmlns:dxe="http://schemas.devexpress.com/winfx/2008/xaml/editors" namespace to the ThemedWindow.
Create a ComboBoxEditSettings object and assign it to the ShipVia column’s ColumnBase.EditSettings property.
Specify the ItemsSource property to bind the ComboBoxEditSettings to data.
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">
<dxg:GridColumn.EditSettings>
<dxe:ComboBoxEditSettings ItemsSource="{Binding Shippers}"
DisplayMember="CompanyName"
ValueMember="ShipperId"/>
</dxg:GridColumn.EditSettings>
</dxg:GridColumn>
Refer to the following help topic for more information about in-place editors: Assign Editors to Cells.
You can configure how the GridControl displays data. The following example formats the Freight column data as currency:
Create a TextEditSettings object and assign it to the Freight column’s ColumnBase.EditSettings property.
Specify the TextEditSettings.MaskType and TextEditSettings.Mask properties to set the editor mask to Currency.
Set the TextEditSettings.MaskUseAsDisplayFormat property to true.
<dxg:GridColumn FieldName="Freight">
<dxg:GridColumn.EditSettings>
<dxe:TextEditSettings Mask="c" MaskType="Numeric"
MaskUseAsDisplayFormat="True"/>
</dxg:GridColumn.EditSettings>
</dxg:GridColumn>
Refer to the following help topic for more information: Format Cell Values.
See Also
Lesson 3 - Post Changes to a Database
Lesson 4 - Sort, Group, Filter Data