Articles in this section
Category / Section

How to format columns while auto-generating columns in WPF DataGrid?

1 min read

In WPF DataGrid, you can format the column by handling the AutoGenerateColumn event that is fired when the columns are auto-generated and the SfDataGrid.AutoGenerateColumns is set to true. AutoGeneratingColumnArgs provides the data for the AutoGenerateColumn event. From the AutoGeneratingColumnArgs.Column, you can get the auto-generated column to format.

this.datagrid.AutoGenerateColumns = true;
this.datagrid.AutoGeneratingColumn+=datagrid_AutoGeneratingColumn;
void datagrid_AutoGeneratingColumn(object sender, AutoGeneratingColumnArgs e)
{
}

 

When the auto-generated column is GridTextColumn, you can format by setting the DisplayBinding to the column.

void datagrid_AutoGeneratingColumn(object sender, AutoGeneratingColumnArgs e)
{
    if (e.Column.MappingName == "EmployeeSalary")
    {
        e.Column.DisplayBinding = new Binding()
        {
            Path = new PropertyPath("EmployeeSalary"),
            StringFormat = "{0:C}"
        };
    }
}

 

When the auto-generated column is GridNumericColumn, GridDateTimeColumn, or GridPercentColumn, the column can be formatted by using the properties exposed to that particular column.

For example, in the following code example, auto-generated GridNumericColumn is formatted by using the GridNumericColumn.NumberDecimalDigits property.

void datagrid_AutoGeneratingColumn(object sender, AutoGeneratingColumnArgs e)
{
    if(e.Column is GridNumericColumn)
        (e.Column as GridNumericColumn).NumberDecimalDigits = 2;
}

 

To apply custom format to the auto-generated column, set the DataTemplate to e.Column.CellTemplate.

void datagrid_AutoGeneratingColumn(object sender, AutoGeneratingColumnArgs e)
{
    if (e.Column.MappingName == "EmployeeSalary" )
    {
        var textblock = new FrameworkElementFactory(typeof(TextBlock));
        var binding = new Binding
        {
            Path = new PropertyPath("EmployeeSalary"),
            StringFormat = "{0:C}"
        };
        textblock.SetBinding(TextBlock.TextProperty, binding);
        DataTemplate datatemplate = new DataTemplate();
        datatemplate.VisualTree = textblock;
        e.Column.CellTemplate = datatemplate;
    }
}

 

Sample Link:

WPF: https://www.syncfusion.com/downloads/support/directtrac/141004/ze/WPF1691503399


Conclusion

I hope you enjoyed learning about how to format the columns while auto-generating the columns in the WPF SfDataGrid.

You can refer to our WPF DataGrid featuretour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. You can also explore our WPF DataGrid demo to understand how to create and manipulate data.

For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.

If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forumsDirect-Trac, or feedback portal. We are always happy to assist you!

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please sign in to leave a comment
Access denied
Access denied