Khadaji
Khadaji

Reputation: 2167

DataSource for User Control

I am buidling a user control. Currently it consists of a textbox and a button - as a learning experience. This will be used as a basis for a more useful control.

I want to add a DataSource, display member and ValueMember.
Here is my code for the datasource. It will display in the Properties editor, but is disabled and grayed out. What am I missing?

    private object MyDataSource;

    [Browsable(true)]
    [TypeConverter("System.Windows.Forms.Design.DataSourceConverter")]
    [System.ComponentModel.Bindable(true)]
    public object DataSource
    {
        get
        {
            return MyDataSource;
        }
        set
        {
            if (MyDataSource != value)
                MyDataSource = value;
        }
    }

Upvotes: 12

Views: 7772

Answers (2)

Talha Anwer
Talha Anwer

Reputation: 141

I added ComplexBindingProperties attribute to my Control class and added AttributeProvider to my DataSource property

[ComplexBindingProperties("DataSource", "DataMember")]
public partial class SomeListControl : UserControl

...

[Category("Data")]
[Description("Indicates the source of data for the control.")]
[RefreshProperties(RefreshProperties.Repaint)]
[AttributeProvider(typeof(IListSource))]
public object DataSource
{
    get { return _ultraGrid.DataSource; }
    set { _ultraGrid.DataSource = value; }
}

[Category("Data")]
[Description("Indicates a sub-list of the data source to show in the control.")]
[Editor("System.Windows.Forms.Design.DataMemberListEditor, System.Design", typeof(UITypeEditor))]    
public string DataMember
{
    get
    {
        return _ultraGrid.DataMember;
    }

    set
    {
        _ultraGrid.DataMember = value;
    }
}

Ref: Apply Attributes in Windows Forms Controls

Upvotes: 5

Marc Gravell
Marc Gravell

Reputation: 1062550

An easier option may be to use an attribute provider instead:

[AttributeProvider(typeof(IListSource))]

You could try using the assembly-qualified name? and specifying the editor?

[TypeConverter("System.Windows.Forms.Design.DataSourceConverter, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")]
[Editor("System.Windows.Forms.Design.DataSourceListEditor, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", "System.Drawing.Design.UITypeEditor, System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")]

Actually, you can abbreviate this to be version-independent just by specifying the assembly:

[TypeConverter("System.Windows.Forms.Design.DataSourceConverter, System.Design")]
[Editor("System.Windows.Forms.Design.DataSourceListEditor, System.Design", typeof(UITypeEditor))]

Upvotes: 6

Related Questions