Create Columns and Bind Them to Data Properties

  • 3 minutes to read

When the GridControl is bound to a data source, you need to create columns and bind them to data properties. There are two ways to do this:

Create and Bind Columns at Design Time

To access the grid's GridControl.Columns collection, invoke the Columns edit form:

CreatingColumns_DesignTime

This form allows you to add, delete, access and customize column settings, and perform other common collection management tasks.

The following code shows how to create columns in XAML:

  • using the GridColumn.FieldName property.

    <dxg:GridControl x:Name="grid">
        <dxg:GridControl.Columns>
            <dxg:GridColumn FieldName="ProductName"/>
            <dxg:GridColumn FieldName="UnitPrice"/>
        </dxg:GridControl.Columns>
        <dxg:GridControl.View>
            <dxg:TableView />
        </dxg:GridControl.View>
    </dxg:GridControl>
    
  • using the GridColumn.Binding property.

    <dxg:GridControl x:Name="grid"> 
        <dxg:GridControl.Columns> 
            <dxg:GridColumn Header="Unit Price" Binding="{Binding UnitPrice, Mode=TwoWay}"/>
            <dxg:GridColumn Header="Product Name" Binding="{Binding ProductName, Mode=TwoWay}"/>
        </dxg:GridControl.Columns> 
        <dxg:GridControl.View> 
            <dxg:TableView /> 
        </dxg:GridControl.View> 
    </dxg:GridControl>
    
NOTE

When working with data structures that contain Dynamic Objects, use the ColumnBase.Binding property instead of ColumnBase.FieldName. Learn more: Binding Columns to Data Source Fields.

NOTE

Columns that are bound using the ColumnBase.Binding property might work more slowly than columns that use the ColumnBase.FieldName property. Learn more: Binding Columns to Data Source Fields.

Create and Bind Columns at Runtime

The following example shows how to create a column and bind it to a data property:

  • using the GridColumn.FieldName property.

    using DevExpress.Xpf.Grid;
    // ...
    GridColumn column = new GridColumn();
    column.FieldName = "ProductName";
    grid.Columns.Add(column);
    
  • using the GridColumn.Binding property.

    using DevExpress.Xpf.Grid;
    // ... 
    GridColumn column = new GridColumn();
    Binding bindingExpression = new Binding("ProductName") { Mode=BindingMode.TwoWay };
    column.Binding = bindingExpression;
    gridControl1.Columns.Add(column);
    

To create columns for all properties in a data source, call the DataControlBase.PopulateColumns method. This method clears the grid's GridControl.Columns collection, and re-populates it with columns/card fields that correspond to all properties in a data source.

See Also