Creating a Bindable Control

I've been searching and can't find anything on the subject.  I'd like to make a control that has a DataSource, ValueMember & DisplayMember just like all the regular WindowsForms Controls.  Is there a standard way of doing this   Any links to anything would really be great.  Seems like everything I find that might be useful is for ASP.NET Controls  :( 

<edit>

Also, I noticed if I declare a DataSource As Object Property, it's always disabled in the Property window.  I'm guessing since it doesn't know what to do with an Object, so my guess is that you have to have a TypeConverter for that Property, but how would I get the drop down to show all bindable sources   Any articles on this sort of thing

</edit>


Answer this question

Creating a Bindable Control

  • CreweAlex

    heh, beats me...  I found it all through the Object Browser..

  • bswaters

    Ok, I found the problem. It does work your way, however, changing

    <TypeConverter("System.Windows.Forms.Design.DataSourceConverter, System.Design")>

    into

    <TypeConverter(System.Windows.Forms.Design.DataSourceConverter)>

    and including a reference to System.Design (which should work and have the added advantage of being type-safe), gives the following compiler error:

        'System.Windows.Forms.Design.DataSourceConverter' is inaccessible due to its protection level

    So I guess the class does exist, but is probably internal or something. TypeConverter seems to have little trouble getting to the class definition by it's string name. The visibility level of this class also explains why it doesn't show up in the object browser or the help.

    Wierd that the crew at Microsoft made the webform variant public and the winform one not...

  • Dados

    Unless you add a reference to the System.Design.DLL
  • Darshita

    Maybe because in the namespace System.Windows.Forms.Design no objects like DataMemberListEditor exist
  • Dmorris

    Yah, I noticed that a lot of the DataBinding Designer stuff is more accessible in ASP.NET than WindowsForms and I was curious too, but it's ok as the way Jacob posted works!  :) 
  • telugodu

    Anything that Implements IList will show up in the Drop Down List for the DataSource Property
  • WinFormsUser13232

    If i want to allow different object types to be bound to my control ,
    is there ANY support for that or do i have to hardcode what interfaces should be allowed to be bound

    i mean , you can bind an arraylist to a datagrid.

    if i want to do the same in my control , do i have to specify that IList is a valid interface and then use reflection to get the propps on the items 

    //Roger

  • Christina LW

    I'm really not sure what you're talking about.  I was able to cut and paste Jacob's original code with minimal changes, added the reference to the System.Design.DLL and it worked no problem.  I am also using .NET 1.1

    Make sure you pasted the code exactly the same.

    If you can't get it working, post the code you're using and we'll see if we can reproduce the error, because it's working just fine here.

  • Victor Irzak

    maybe i'm not understanding your question.  when you implement IList, it has everything you need to implement in it to make all of that happen.  Just try it and see what happens   :) 
  • leabre

    I found these items by looking at other implementations of similar properties in the object browser (you can see the declarations, including attributes, in the bottom pane).  As to whether or not these items are "supported," I honestly don't know.  However, they do seem to work.  Perhaps a member of the Windows Forms team can jump in and provide some information on these items.
  • NiklasL

    yes but if i want to extract columns form the contents etc , so i can get the same behavior from an ilist collection as from  a datatable.. 

    (so i a list of rows and each row has columns (which in the case of ilist content would be properties of the stored objects)

    there is no built in way to do that , right
    i will have to use reflection when i deal with objects in the ilist and read the fields of rows when reading from the datatable right

    //Roger


  • trhodes

    Thanks a million, Jacob.  I'll give that all a go!  Now why in the heck couldn't I find that stuff in the documentation
  • John Mac

    I tried adding the reference as you suggest, but that doesn't help. According to the help, the System.Windows.Forms.Design namespace does not contain any of these and a search only comes up with the web ones, which gives me some serious doubts about their existance (at least they're undocumented). Also the object browser doesn't show them...
    I am using the 1.1 version of the framework, maybe Microsoft took it out and that's the problem  Otherwise, I don't have a clue.
    I you have any further suggestions, I would be very grateful.

    Bas.

  • Stalkers

    Test this out and see if it works.  It's mostly air-code but I think it will at least get you started:


    Public Class MyControl
        Inherits Control

        Private m_DataSource As Object
        Private m_DataMember As String
        Private m_ValueMember As String
        Private m_DisplayMember As String

        <RefreshProperties(RefreshProperties.All), TypeConverter("System.Windows.Forms.Design.DataSourceConverter, System.Design")> _
        Public Property DataSource() As Object
            Get
                Return m_DataSource
            End Get
            Set(ByVal Value As Object)
                m_DataSource = Value
            End Set
        End Property

        <Editor("System.Windows.Forms.Design.DataMemberListEditor, System.Design", "System.Drawing.Design.UITypeEditor, System.Drawing")> _
        Public Property DataMember() As String
            Get
                Return m_DataMember
            End Get
            Set(ByVal Value As String)
                m_DataMember = Value
            End Set
        End Property

        <Editor("System.Windows.Forms.Design.DataMemberFieldEditor, System.Design", "System.Drawing.Design.UITypeEditor, System.Drawing")> _
        Public Property ValueMember() As String
            Get
                Return m_ValueMember
            End Get
            Set(ByVal Value As String)
                m_ValueMember = Value
            End Set
        End Property

    <Editor("System.Windows.Forms.Design.DataMemberFieldEditor, System.Design", "System.Drawing.Design.UITypeEditor, System.Drawing"), TypeConverter("System.Windows.Forms.Design.DataMemberFieldConverter")> _
        Public Property DisplayMember() As String
            Get
                Return m_DisplayMember
            End Get
            Set(ByVal Value As String)
                m_DisplayMember= Value
            End Set
        End Property

    End Class

  • Creating a Bindable Control