New to Telerik UI for WinForms? Download free 30-day trial

How to Save/Load Layout with Custom Columns in RadGridView

Environment

Product Version Product Author
2021.3.914 RadGridView for WinForms Desislava Yordanova

Description

RadGridView provides a convenient API for creating custom columns with custom cell elements. However, the XML layout appears to be removing all the columns and related grid data after adding the custom column.

A common requirement is to save the layout with all the columns in the grid and restore this layout at a later moment together with the custom columns.

Solution

For the serialization, the custom column needs to have a parameterless constructor in order to be able to load the layout later:

    public class ProgressBarColumn : GridViewDataColumn
    {
        public ProgressBarColumn()
        {
        }

        public ProgressBarColumn(string fieldName) : base(fieldName)
        {
        }

        public override Type GetCellType(GridViewRowInfo row)
        {
            if (row is GridViewDataRowInfo)
            {
                return typeof(ProgressBarCellElement);
            }
            return base.GetCellType(row);
        }
    }