New to Telerik UI for WinFormsStart a free 30-day trial

How to create an auto-size RadDataEntry

Updated over 6 months ago

Environment

Product VersionProductAuthor
2019.1.117RadDataEntry for WinFormsHristo Merdjanov

Description

The editors and labels displayed in the RadDataEntry are generated dynamically while initializing the control. This article demonstrates how the control can be auto-sized so that it will fit all of added controls without displaying scroll bars.

Figure 1: Auto-sized Data Entry

dataentry-auto-size 001

Solution

The locations of the panels are determined while initializing the controls, besides their size, the location along Y is also controlled by the RadDataEntry.ItemSpace property. A suitable place to adjust the size is the RadDataEntry.ItemInitialized event and in the handler check if the panel`s bounds would exceed the bounds of the control.

ItemInitialized Event

C#
public partial class RadForm1 : Telerik.WinControls.UI.RadForm
{
    public RadForm1()
    {
        InitializeComponent();

        this.radDataEntry1.ItemInitialized += this.RadDataEntry1_ItemInitialized;;
        this.radDataEntry1.FitToParentWidth = true;
        this.radDataEntry1.ItemDefaultSize = new Size(250, 30);
        this.radDataEntry1.ItemSpace = 10;

        this.radDataEntry1.DataSource = new Employee()
        {
            FirstName = "Sarah",
            LastName = "Blake",
            Occupation = "Supplied Manager",
            StartingDate = new DateTime(2005, 04, 12),
            IsMarried = true,
            Salary = 3500,
            Gender = Gender.Female
        };
    }

    private void RadDataEntry1_ItemInitialized(object sender, Telerik.WinControls.UI.ItemInitializedEventArgs e)
    {
        if (e.Panel.Location.Y + e.Panel.Height > this.radDataEntry1.Height)
        {
            this.radDataEntry1.Height += e.Panel.Location.Y + e.Panel.Height - this.radDataEntry1.Height + this.radDataEntry1.ItemSpace;
        }
    }
}

Sample Data Class

C#
public class Employee
{
    public string FirstName
        {
            get;
            set;
        }

    public string LastName
        {
            get;
            set;
        }

    public string Occupation
        {
            get;
            set;
        }

    public DateTime StartingDate
        {
            get;
            set;
        }

    public bool IsMarried
        {
            get;
            set;
        }

    public int Salary
        {
            get;
            set;
        }

    public Gender Gender
        {
            get;
            set;
        }
}

public enum Gender
{
    Female,
    Male
}

See Also