AutoGenerateColumns property automatically generate columns for display in UI from the bounded data source.
It takes every public property in bounded data source to generate columns. For example, if you bind to below Employee class to DataGrid:
public class Employee
{
protected int Property1 { get; set; }
private int Property2 { get; set; }
static int Property3 { get; set; }
public int Property4 { get; set; }
public int Variable1;
}
Only one column is generated for Property4 in the DataGrid because it has public modifier. Rest all properties and variables are ignored.
Set DataGrid AutoGenerateColumns Property
You can set AutoGenerateColumns property in DataGrid both in XAML and code-behind class.
<DataGrid x:Name="myDataGrid" AutoGenerateColumns="True">
</DataGrid>
myDataGrid.AutoGenerateColumns = true;
Type of Generated Columns
By default DataGrid based on the column type generate columns. For example, if you bind to a bool property, then it will generate a checkbox column.
Below is table of data type mapping:
Data Type | Column Generated |
---|---|
bool | Checkbox column |
enum | Combobox column |
Uri | Hyperlink column |
string | Textbox column |
DateTime | Textbox column |
int | Textbox column |
double | Textbox column |
decimal | Textbox column |
AutoGenerateColumn Example
Below is full example of DataGrid auto generation of columns:
<Window x:Class="WpfApplication7.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<DataGrid x:Name="myDataGrid" AutoGenerateColumns="True">
</DataGrid>
</Grid>
</Window>
using System;
using System.Collections.ObjectModel;
using System.Windows;
namespace WpfApplication7
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
ObservableCollection<Employee> employees = new ObservableCollection<Employee>();
employees.Add(new Employee { ID = 1, Name = "Kapil Malhotra", IsMale = true, Type = EmployeeType.Normal, SiteID = new Uri("http://localhost/4322"), BirthDate = new DateTime(1980,1,1) });
employees.Add(new Employee { ID = 2, Name = "George", IsMale = true, Type = EmployeeType.Manager, SiteID = new Uri("http://localhost/4432") , BirthDate = new DateTime(1980, 2, 1) });
employees.Add(new Employee { ID = 3, Name = "Vicky", IsMale = false, Type = EmployeeType.Supervisor, SiteID = new Uri("http://localhost/4872"), BirthDate = new DateTime(1980, 3, 1) });
myDataGrid.ItemsSource = employees;
}
}
public class Employee
{
public int ID { get; set; }
public string Name { get; set; }
public bool IsMale { get; set; }
public EmployeeType Type { get; set; }
public Uri SiteID { get; set; }
public DateTime BirthDate { get; set; }
}
public enum EmployeeType
{
Normal,
Supervisor,
Manager
}
}
In the above code, we have create a new DataGrid name ‘myDataGrid’ and set AutoGenerateColumns property to True.
In the code-behind class, we have create a class ‘Employee’ which has properties of different types like int, string, bool, enum, Uri, DateTime. Then we have created an ObservableCollection of Employee class and create three sample employee type and bind ObservableCollection to DataGrid in the ItemsSource property.
Events
DataGrid provides two events for auto generate columns process.
- AutoGeneratingColumn
- AutoGeneratedColumns
AutoGeneratingColumn event
This event provides DataGridAutoGeneratingColumnEventArgs event argument which contains three important properties:
- Cancel – For cancel the column to be generated
- Column – Generated column
- PropertyName – Get the name of property bound to generated column
You can use this event for two important tasks:
- Remove the column from the being created
- Change/Update the header of generated column
Below is the example of both:
public MainWindow()
{
InitializeComponent();
ObservableCollection<Employee> employees = new ObservableCollection<Employee>();
//load collection
myDataGrid.AutoGeneratingColumn += myDataGrid_AutoGeneratingColumn;
myDataGrid.ItemsSource = employees;
}
void myDataGrid_AutoGeneratingColumn(object sender, System.Windows.Controls.DataGridAutoGeneratingColumnEventArgs e)
{
if (e.PropertyName == "ID")
e.Cancel = true;
if (e.PropertyName == "Name")
{
e.Column.Header = "First Name";
}
}
In the above code, I have checked the PropertyName of event argument. If PropertyName is ‘ID’ then set e.Cancel to true. Then ID column will not be generated.
I have also changed the header of Name column by changed the Header property of Column property of event argument.
AutoGeneratedColumns event
This event occurs when the auto completion process is completed by the DataGrid.
By this event, datagrid Colums property is filled with the generated columns and you can modify any generated column by giving index to Columns property.