Skip to main content

How to: Customize Automatically Generated Columns

  • 3 minutes to read

This example shows how to use the AutoGeneratedColumns event to customize automatically generated columns.

View Example

<Window x:Class="E2019.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:dxg="http://schemas.devexpress.com/winfx/2008/xaml/grid"
        xmlns:dxe="http://schemas.devexpress.com/winfx/2008/xaml/editors"
        xmlns:local="clr-namespace:E2019"
        Title="Window" Height="200" Width="470">
    <Window.Resources>
        <local:IssueTypeForegroundConverter x:Key="IssueTypeForegroundConverter"/>
        <DataTemplate x:Key="IssueNameTemplate">
            <dxe:TextEdit x:Name="PART_Editor" Foreground="Blue"/>
        </DataTemplate>
        <DataTemplate x:Key="IssueTypeTemplate">
            <dxe:TextEdit x:Name="PART_Editor" Foreground="{Binding Path=Value, Converter={StaticResource IssueTypeForegroundConverter}}"/>
        </DataTemplate>
    </Window.Resources>
    <Grid>
       <dxg:GridControl x:Name="grid" AutoGenerateColumns="AddNew" AutoGeneratedColumns="OnColumnsGenerated">
          <dxg:GridControl.View>
             <dxg:TableView Name="tableView1" AutoWidth="True" />
          </dxg:GridControl.View>
       </dxg:GridControl>
    </Grid>
</Window>
using System.Collections.Generic;
using System.Windows;
using System.Windows.Documents;
using DevExpress.Xpf.Grid;
using System.Windows.Data;
using System.Windows.Media;

namespace E2019 {
    public partial class Window1 : Window {
        public Window1() {
            InitializeComponent();
            grid.ItemsSource = IssueList.GetData();
        }
        private void OnColumnsGenerated(object sender, RoutedEventArgs e) {
            foreach (GridColumn column in grid.Columns) {
                switch (column.FieldName) {
                    case "IssueName":
                        column.CellTemplate = Application.Current.MainWindow.Resources["IssueNameTemplate"] as DataTemplate;
                        column.SortOrder = DevExpress.Data.ColumnSortOrder.Ascending;
                        break;
                    case "IssueType":
                        column.CellTemplate = Application.Current.MainWindow.Resources["IssueTypeTemplate"] as DataTemplate;
                        break;
                    case "ID":
                        column.Visible = false;
                        break;
                }
            }
        }
        public class IssueList {
            static public List<IssueDataObject> GetData() {
                List<IssueDataObject> data = new List<IssueDataObject>();
                data.Add(new IssueDataObject() {
                    ID = 0,
                    IssueName = "Transaction History", IssueType = "Bug", IsPrivate = true
                });
                data.Add(new IssueDataObject() {
                    ID = 1,
                    IssueName = "Ledger: Inconsistency", IssueType = "Bug", IsPrivate = false
                });
                data.Add(new IssueDataObject() {
                    ID = 2,
                    IssueName = "Data Import", IssueType = "Request", IsPrivate = false
                });
                data.Add(new IssueDataObject() {
                    ID = 3,
                    IssueName = "Data Archiving", IssueType = "Request", IsPrivate = true
                });
                return data;
            }
        }
        public class IssueDataObject {
            public int ID { get; set; }
            public string IssueName { get; set; }
            public string IssueType { get; set; }
            public bool IsPrivate { get; set; }
        }
    }

    public class IssueTypeForegroundConverter : IValueConverter {
        public object Convert(object value, System.Type targetType, object parameter, System.Globalization.CultureInfo culture) {
            if (value == null)
                return null;

            string issueType = value.ToString();
            if (issueType == "Bug")
                return new SolidColorBrush(Colors.Red);

            return new SolidColorBrush(Colors.Black);
        }

        public object ConvertBack(object value, System.Type targetType, object parameter, System.Globalization.CultureInfo culture) {
            throw new System.NotImplementedException();
        }
    }

}