Attention: We are retiring the ASP.NET Community Blogs. Learn more >

Data Source Windows Tips & Trics

How to: Bind a Custom Control type to class public property in Data Source Windows

Step 1: Build your object

public class Machine
    {
        private string model;
        private string id;
        private decimal price;
        private Part part;

        public Machine()
        {
            part = new Part();
            parts = new List(); 
        }
        [Bindable(true)]
        public string Model
        {
            get { return model; }
            set { model = value; }
        }
        [Bindable(true)]
        public string Id
        {
            get { return id; }
            set { id = value; }
        }
        [Bindable(true)]
        public decimal Price
        {
            get { return price; }
            set { price = value; }
        } 
        [Bindable(true)]
        public Part Part
        {
            get { return part; }
        }
    }
    public class Part
    {
        private string partId;
        public string PartId
        {
            get { return partId; }
            set { partId = value; }
        }

        private string name;
        public string Name
        {
            get { return name; }
            set { name = value; }
        }
    }

Step 2: Build your UserControl ( for Part object )
    [DefaultBindingProperty("Part")]
    public partial class PartControl : UserControl
    {
        private Part m_Part;
        public Part Part
        {
            get { return m_Part; }
            set 
            { 
                m_Part = value;
                if ( !DesignMode && value != null )
                    partBindingSource.DataSource = value;
            }
        }

        public PartControl()
        {
            InitializeComponent();
            partBindingSource.DataSource = typeof(Part);
            txbName.DataBindings.Add  ("Text", partBindingSource, "Name");
            txbPartID.DataBindings.Add("Text", partBindingSource, "PartId");
        }
    }
Step 3: Customize the part property to the PartControl

* Open Data Source windows
* Select new data source
* Select Machine class
* Select Part property
* Right Click --> Customize --> Open Option Windows
* Select Other
* Select PartControl --> OK
* Select Part Property--> Right-Click--> Select PartControl
* Select Machine-->Right-Click--> Select Details
=================================
Drag and Drop the Machine to the form......
This demo for 1:1 for 1:M use ComplexBindingProperties or LookupBindingProperties attribute

1 Comment

Comments have been disabled for this content.