locked
How to implement Datasource property on custom controls? RRS feed

  • Question

  • Im building a custom control called DataEntryControl and want to be able to
    setup a DataSource on property window, same way as DataGridView...

    Here are some parts of my code:

    Public Class DataEntryControl

        Private _DataSource As IListSource
        <Category("Data")> _
        Public Property DataSource() As IListSource
            Get
                Return _DataSource
            End Get
            Set(ByVal value As IListSource)
                _DataSource = value
                SetupControls()
            End Set
        End Property

        Private _DataMember As String
        <Category("Data")> _
        Public Property DataMember() As String
            Get
                Return _DataMember
            End Get
            Set(ByVal value As String)
                _DataMember = value
            End Set
        End Property

    ...

    End Class


    The DataSource and DataMember properties are being displayed on property
    window, the DataSource is being displayed same way as DataGridView DataSource property the way I want it...

    When I try to set the DataSource property on the property window I get the following error:
    Object of type 'System.Windows.Forms.BindingSource' cannot be converted to type 'System.ComponentModel.IListSource'

    I tried to set it to an existing BindindingSource and to a collection of BusinessObjects derived from BindingList, both give me this error.

    What am I missing?

    Thanks in advance.

    Fernando

    Tuesday, December 6, 2005 4:12 AM

Answers

  • Very thanks for your answer, I learned some stuff from this link.

    But it don't solved the problem, it's a bit different on dotnet 2.0.

    I found an answer to this on a newsgroup, here is the code Big Smile

    Private _DataSource As Object
    <Category("Data"), AttributeProvider("System.ComponentModel.IListSource")> _ Public Property DataSource() As Object
       Get
          Return _DataSource
       End Get

       Set(ByVal value As Object)
          _DataSource = value
       End Set
    End Property

    Private _DataMember As String
    <Category("Data"), Editor("System.Windows.Forms.Design.DataMemberListEditor, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", "System.Drawing.Design.UITypeEditor")> _ Public Property DataMember() As String
       Get
          Return _DataMember
       End Get

       Set(ByVal value As String)
          _DataMember = value
       End Set
    End Property

    Best regards.

    Fernando Cardoso

    Wednesday, December 7, 2005 11:58 PM

All replies

  • Define the DataSource property as Object. BindingSource does not implement IListSource, which causes the exception.

    Refer to the DataGridView.DataSource property in the documentation about what kinds of data sources you should expect (and use appropriately).

    Good luck!

    Jordan Jossifov
    Tuesday, December 6, 2005 1:24 PM
  • Thanks for your answer.

    Yeah, I looked at it before, I knew it doesn't implements IListSource. Looked at DataGridView.DataSource property also and its stored as Object.

    I should be missing something, like a property Attribute... I know I'm close to the solution, but don't realized how to make it yet.

    Any more clues?

    Best regards,

    Fernando Cardoso
    Tuesday, December 6, 2005 1:29 PM
  • Correct, you missed something Smile. Here is what you need:

    http://msdn.microsoft.com/smartclient/understanding/windowsforms/customcontrols/default.aspx?pull=/library/en-us/dnadvnet/html/vbnet08262002.asp


    It took some time to find it...

    Good luck!

    Jordan Jossifov

    Wednesday, December 7, 2005 12:53 PM
  • Very thanks for your answer, I learned some stuff from this link.

    But it don't solved the problem, it's a bit different on dotnet 2.0.

    I found an answer to this on a newsgroup, here is the code Big Smile

    Private _DataSource As Object
    <Category("Data"), AttributeProvider("System.ComponentModel.IListSource")> _ Public Property DataSource() As Object
       Get
          Return _DataSource
       End Get

       Set(ByVal value As Object)
          _DataSource = value
       End Set
    End Property

    Private _DataMember As String
    <Category("Data"), Editor("System.Windows.Forms.Design.DataMemberListEditor, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", "System.Drawing.Design.UITypeEditor")> _ Public Property DataMember() As String
       Get
          Return _DataMember
       End Get

       Set(ByVal value As String)
          _DataMember = value
       End Set
    End Property

    Best regards.

    Fernando Cardoso

    Wednesday, December 7, 2005 11:58 PM
  • Hi Fernando,

     

    I am building a list box that needs to display data in a similar way to how Outlook shows its list of emails - so I want to have a number of data bound properties (DateMember, SummaryMember, as well as the standard DisplayMember and ValueMember). I have tried your suggestions but rather than getting fields from the datasource, I get a list of foreign keys that exist within the table selected in the datasource. The inherited DisplayMember property correctly shows the list of fields within the table. Do you have any ideas as to why this would happen?

     

    Here is my code...

     

    Public Class ListBoxExtended

        Inherits ListBox

        Private mDateMember As String = ""

     

        Public Sub New()

            Me.DrawMode = Windows.Forms.DrawMode.OwnerDrawFixed

            Me.ItemHeight = 36

        End Sub

     

        <Category("Data"), Editor("System.Windows.Forms.Design.DataMemberListEditor, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", "System.Drawing.Design.UITypeEditor")> _

        Public Property DateMember() As String

            Get

                Return mDateMember

            End Get

            Set(ByVal value As String)

                mDateMember = value

            End Set

        End Property

     

        Protected Overrides Sub OnDrawItem(ByVal e As System.Windows.Forms.DrawItemEventArgs)

            Dim brush As New System.Drawing.SolidBrush(e.ForeColor)

            e.DrawBackground()

            e.Graphics.DrawLine(New Pen(Color.FromArgb(227, 239, 255)), e.Bounds.X, e.Bounds.Bottom - 1, e.Bounds.Right, e.Bounds.Bottom - 1)

            e.Graphics.DrawString("Test", e.Font, brush, e.Bounds.X + 7, e.Bounds.Y + 7)

            e.DrawFocusRectangle()

        End Sub

    End Class

    • Proposed as answer by MuraliAsok Saturday, October 17, 2009 5:25 AM
    Tuesday, May 15, 2007 1:47 PM
  •  fjcardoso wrote:
    Very thanks for your answer, I learned some stuff from this link.

    I am searaching some Property Attribute of my custom controll which can give me navigation of coloums in some datatable of dataset on my from
    I have tried this
    <Category("Data"), AttributeProvider("System.ComponentModel.IListSource")> _ Public Property DataSource() As Object

    it gives me datasets in my form

    i also have tried
    <Category("Data"), Editor("System.Windows.Forms.Design.DataMemberListEditor, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", "System.Drawing.Design.UITypeEditor")> _ Public Property DataMember() As String
       Get
    it gives me tables in a dataset on my form

    but i am still looking for some Attribute which can give me Coloumns in some table of dataset

    can some body help me to find this

    Thank you
    Thursday, June 7, 2007 12:28 PM
  • I am searaching some Property Attribute of my custom controll which can give me navigation of coloums in some datatable of dataset on my from
    I have tried this
    <Category("Data"), AttributeProvider("System.ComponentModel.IListSource")> _ Public Property DataSource() As Object

    it gives me datasets in my form

    i also have tried
    <Category("Data"), Editor("System.Windows.Forms.Design.DataMemberListEditor, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", "System.Drawing.Design.UITypeEditor")> _ Public Property DataMember() As String
       Get
    it gives me tables in a dataset on my form

    but i am still looking for some Attribute which can give me Coloumns in some table of dataset

    can some body help me to find this

    Thank you
    Thursday, June 7, 2007 12:29 PM
  •  

    Hi,

     

    I have a vb6 application. I need to create textbox controls in vb.net 2008 to be used in an existing application developed in vb6. I will replace the old textboxes with the new .net textboxes. I havea difficulty in implementing the following properties in .net.

     

    1. TextBox.DataSource      ' as DataSource is not a binding property in .net therefore I have to implement it but don't know how.

    2. DataField and DataMember ' Same rule apply

    3. BackColor/ForeColor which is not displayed in vb6 when I place the control on the form.

    Tuesday, March 25, 2008 2:37 PM
  •  

    Hi,

     

    I have a vb6 application. I need to create textbox controls in vb.net 2008 to be used in an existing application developed in vb6. I will replace the old textboxes with the new .net textboxes. I havea difficulty in implementing the following properties in .net.

     

    1. TextBox.DataSource      ' as DataSource is not a binding property in .net therefore I have to implement it but don't know how.

    2. DataField and DataMember ' Same rule apply

    3. BackColor/ForeColor which is not displayed in vb6 when I place the control on the form.

     

    Any one to help Please ???

    I would really appreciate your help.
    Tuesday, March 25, 2008 2:38 PM
  • look here http://www.codeproject.com/Articles/15396/Implementing-complex-data-binding-in-custom-contro
    Friday, September 4, 2015 3:10 PM