UserControl: Databinding

M

Michael Maes

Hi,

I have a UserControl containing some controls of which one is a ComboBox.
All the InternalControls are Private and some are allowed to be accessed through Public Methods.
One of the things I would like to do (of course) is set the DataBindings.
Something clearly is wrong with the approach I use, because I can't set the Properties in the Designer, the DataSource-Property appears like 'ReadOnly', there is no "dropdown" with the list from System.Windows.Forms.ListControl

Dim _DataSource As Object
<EditorBrowsable(EditorBrowsableState.Always), _
Browsable(True), _
Description("The data source for the dropdown list."), Category("Data")> _
Public Overridable Property DataSource() As Object
Get
Return _DataSource
End Get
Set(ByVal Value As Object)
_DataSource = Value
Me.Internal_UiComboBox.DataSource = Value
End Set
End Property

Dim _DisplayMember As String
<EditorBrowsable(EditorBrowsableState.Always), _
Browsable(True), _
Description("Specifies the property of the data source whose content you want to display."), Category("Data")> _
Public Overridable Property DisplayMember() As String
Get
Return _DisplayMember
End Get
Set(ByVal Value As String)
_DisplayMember = Value
Me.Internal_UiComboBox.DisplayMember = Value
End Set
End Property


In the Advanced Wizard I can Set a Value, only, I can't set a DataSet, only a DataSet.DataTable.DataColumn.

What is wrong with my approach?

TIA,

Michael
 
J

Jared

I'm just guessing here, so take this for what it's worth; instead of using an object could you try to bind to some IEnumerable or other interface object? That way you know the datasource is supported and the designer know what type(s) of objects to expect. I don't know if IEnumerable is the right interface for you but it will work in this example.

Dim DataSource as IEnumerable

Public Overridable Property DataSource() As IEnumerable
Get
Return _DataSource
End Get
Set(ByVal Value As IEnumerable)
_DataSource = Value
Me.Internal_UiComboBox.DataSource = Value
End Set
End Property

in your project you could do something like

Dim ds As New DataSet
ds.ReadXml("C:\Path_to_some_xml_file\MyFile.xml")
'DataSet doesn't support IEnumerable, DataView does
Me.MyUserConrol.DataSource = ds.Tables(0).DefaultView
Me.MyUserControl.DisplayMember = "SomeProperty"

-- or --

The designer will display all IEnumerable interfaces and allow you to choose a datasource that will work with your object.

I looked up the combo box in the Object browser, it looks like it supports IList, IEnumerable and ICollection. Both IList and ICollection implement IEnumerable.

Hope this helps.
Jared


Hi,

I have a UserControl containing some controls of which one is a ComboBox.
All the InternalControls are Private and some are allowed to be accessed through Public Methods.
One of the things I would like to do (of course) is set the DataBindings.
Something clearly is wrong with the approach I use, because I can't set the Properties in the Designer, the DataSource-Property appears like 'ReadOnly', there is no "dropdown" with the list from System.Windows.Forms.ListControl

Dim _DataSource As Object
<EditorBrowsable(EditorBrowsableState.Always), _
Browsable(True), _
Description("The data source for the dropdown list."), Category("Data")> _
Public Overridable Property DataSource() As Object
Get
Return _DataSource
End Get
Set(ByVal Value As Object)
_DataSource = Value
Me.Internal_UiComboBox.DataSource = Value
End Set
End Property

Dim _DisplayMember As String
<EditorBrowsable(EditorBrowsableState.Always), _
Browsable(True), _
Description("Specifies the property of the data source whose content you want to display."), Category("Data")> _
Public Overridable Property DisplayMember() As String
Get
Return _DisplayMember
End Get
Set(ByVal Value As String)
_DisplayMember = Value
Me.Internal_UiComboBox.DisplayMember = Value
End Set
End Property


In the Advanced Wizard I can Set a Value, only, I can't set a DataSet, only a DataSet.DataTable.DataColumn.

What is wrong with my approach?

TIA,

Michael
 
M

Michael Maes

Hi Jared,

Thanks for your reply.
This won't do the trick because I want my control to be able to bind to ILists and ICollections. As I want to be able to set the Datasource & the Datamember apart, I can't bind to a DataSource in this case.

I want my bindings set like: (both in the Property-Designer as in Run--Time)

With UC
.DataSource = DataSource (eg DataSet) --> This wont work.
.DataMember = "DataTable"
.DisplayMember = "Column"
.ValueMember = "Column"
End With

Regards,

Michael

I'm just guessing here, so take this for what it's worth; instead of using an object could you try to bind to some IEnumerable or other interface object? That way you know the datasource is supported and the designer know what type(s) of objects to expect. I don't know if IEnumerable is the right interface for you but it will work in this example.

Dim DataSource as IEnumerable

Public Overridable Property DataSource() As IEnumerable
Get
Return _DataSource
End Get
Set(ByVal Value As IEnumerable)
_DataSource = Value
Me.Internal_UiComboBox.DataSource = Value
End Set
End Property

in your project you could do something like

Dim ds As New DataSet
ds.ReadXml("C:\Path_to_some_xml_file\MyFile.xml")
'DataSet doesn't support IEnumerable, DataView does
Me.MyUserConrol.DataSource = ds.Tables(0).DefaultView
Me.MyUserControl.DisplayMember = "SomeProperty"

-- or --

The designer will display all IEnumerable interfaces and allow you to choose a datasource that will work with your object.

I looked up the combo box in the Object browser, it looks like it supports IList, IEnumerable and ICollection. Both IList and ICollection implement IEnumerable.

Hope this helps.
Jared


Hi,

I have a UserControl containing some controls of which one is a ComboBox.
All the InternalControls are Private and some are allowed to be accessed through Public Methods.
One of the things I would like to do (of course) is set the DataBindings.
Something clearly is wrong with the approach I use, because I can't set the Properties in the Designer, the DataSource-Property appears like 'ReadOnly', there is no "dropdown" with the list from System.Windows.Forms.ListControl

Dim _DataSource As Object
<EditorBrowsable(EditorBrowsableState.Always), _
Browsable(True), _
Description("The data source for the dropdown list."), Category("Data")> _
Public Overridable Property DataSource() As Object
Get
Return _DataSource
End Get
Set(ByVal Value As Object)
_DataSource = Value
Me.Internal_UiComboBox.DataSource = Value
End Set
End Property

Dim _DisplayMember As String
<EditorBrowsable(EditorBrowsableState.Always), _
Browsable(True), _
Description("Specifies the property of the data source whose content you want to display."), Category("Data")> _
Public Overridable Property DisplayMember() As String
Get
Return _DisplayMember
End Get
Set(ByVal Value As String)
_DisplayMember = Value
Me.Internal_UiComboBox.DisplayMember = Value
End Set
End Property


In the Advanced Wizard I can Set a Value, only, I can't set a DataSet, only a DataSet.DataTable.DataColumn.

What is wrong with my approach?

TIA,

Michael
 
J

Jared

Michael,
Forgive my ignorance, but, doesn't the designer only support strong types? Now I am curious to see the answer.
Jared
Hi Jared,

Thanks for your reply.
This won't do the trick because I want my control to be able to bind to ILists and ICollections. As I want to be able to set the Datasource & the Datamember apart, I can't bind to a DataSource in this case.

I want my bindings set like: (both in the Property-Designer as in Run--Time)

With UC
.DataSource = DataSource (eg DataSet) --> This wont work.
.DataMember = "DataTable"
.DisplayMember = "Column"
.ValueMember = "Column"
End With

Regards,

Michael

I'm just guessing here, so take this for what it's worth; instead of using an object could you try to bind to some IEnumerable or other interface object? That way you know the datasource is supported and the designer know what type(s) of objects to expect. I don't know if IEnumerable is the right interface for you but it will work in this example.

Dim DataSource as IEnumerable

Public Overridable Property DataSource() As IEnumerable
Get
Return _DataSource
End Get
Set(ByVal Value As IEnumerable)
_DataSource = Value
Me.Internal_UiComboBox.DataSource = Value
End Set
End Property

in your project you could do something like

Dim ds As New DataSet
ds.ReadXml("C:\Path_to_some_xml_file\MyFile.xml")
'DataSet doesn't support IEnumerable, DataView does
Me.MyUserConrol.DataSource = ds.Tables(0).DefaultView
Me.MyUserControl.DisplayMember = "SomeProperty"

-- or --

The designer will display all IEnumerable interfaces and allow you to choose a datasource that will work with your object.

I looked up the combo box in the Object browser, it looks like it supports IList, IEnumerable and ICollection. Both IList and ICollection implement IEnumerable.

Hope this helps.
Jared


Hi,

I have a UserControl containing some controls of which one is a ComboBox.
All the InternalControls are Private and some are allowed to be accessed through Public Methods.
One of the things I would like to do (of course) is set the DataBindings.
Something clearly is wrong with the approach I use, because I can't set the Properties in the Designer, the DataSource-Property appears like 'ReadOnly', there is no "dropdown" with the list from System.Windows.Forms.ListControl

Dim _DataSource As Object
<EditorBrowsable(EditorBrowsableState.Always), _
Browsable(True), _
Description("The data source for the dropdown list."), Category("Data")> _
Public Overridable Property DataSource() As Object
Get
Return _DataSource
End Get
Set(ByVal Value As Object)
_DataSource = Value
Me.Internal_UiComboBox.DataSource = Value
End Set
End Property

Dim _DisplayMember As String
<EditorBrowsable(EditorBrowsableState.Always), _
Browsable(True), _
Description("Specifies the property of the data source whose content you want to display."), Category("Data")> _
Public Overridable Property DisplayMember() As String
Get
Return _DisplayMember
End Get
Set(ByVal Value As String)
_DisplayMember = Value
Me.Internal_UiComboBox.DisplayMember = Value
End Set
End Property


In the Advanced Wizard I can Set a Value, only, I can't set a DataSet, only a DataSet.DataTable.DataColumn.

What is wrong with my approach?

TIA,

Michael
 
E

Ernest Morariu

I'm still trying to figure out how to list the DisplayMembers &
ValueMembers
in the Dropdown of the Property-Designer.

<Category("Some Category"),DefaultValueAttribute(""),
RefreshProperties(RefreshProperties.Repaint),
Editor(typeof(ExtendedDataGrid.Utilities.DataMemberFieldEditor),
typeof(System.Drawing.Design.UITypeEditor))> _
Public Property DisplayMember() as String
'...
End Property

Ernest



Hi Jared,

Of course you're right. Maybe I didn't express myself too clear. (which
often happens, so I've added a pic to clarify)
If I declare the Property as an IEnumerable, then I get an imlpicit
TypeConversion Error since a DataSet doesn't implement the IEnumerable
interface.

Bit by bit the puzzle is "coming to pieces" and I've got the DataSource
(Object) & the DataMember-Properties working just fine now (I used a
TypeConverter and a UITypeEditor). I'm still trying to figure out how to
list the DisplayMembers & ValueMembers in the Dropdown of the
Property-Designer.

So basically my issue is one of the IDE. I can set my properties through
code without any problem, but I want a developer to be able to select the
available DataSources, DataMembers and DataColums in a Dropdown......

Regards,

Michael
Michael,
Forgive my ignorance, but, doesn't the designer only support strong
types? Now I am curious to see the answer.
Jared
Hi Jared,

Thanks for your reply.
This won't do the trick because I want my control to be able to bind to
ILists and ICollections. As I want to be able to set the Datasource & the
Datamember apart, I can't bind to a DataSource in this case.

I want my bindings set like: (both in the Property-Designer as in
Run--Time)

With UC
.DataSource = DataSource (eg DataSet) --> This wont work.
.DataMember = "DataTable"
.DisplayMember = "Column"
.ValueMember = "Column"
End With

Regards,

Michael

I'm just guessing here, so take this for what it's worth; instead of
using an object could you try to bind to some IEnumerable or other interface
object? That way you know the datasource is supported and the designer know
what type(s) of objects to expect. I don't know if IEnumerable is the right
interface for you but it will work in this example.

Dim DataSource as IEnumerable

Public Overridable Property DataSource() As IEnumerable
Get
Return _DataSource
End Get
Set(ByVal Value As IEnumerable)
_DataSource = Value
Me.Internal_UiComboBox.DataSource = Value
End Set
End Property

in your project you could do something like

Dim ds As New DataSet
ds.ReadXml("C:\Path_to_some_xml_file\MyFile.xml")
'DataSet doesn't support IEnumerable, DataView does
Me.MyUserConrol.DataSource = ds.Tables(0).DefaultView
Me.MyUserControl.DisplayMember = "SomeProperty"

-- or --

The designer will display all IEnumerable interfaces and allow you to
choose a datasource that will work with your object.

I looked up the combo box in the Object browser, it looks like it
supports IList, IEnumerable and ICollection. Both IList and ICollection
implement IEnumerable.

Hope this helps.
Jared


Hi,

I have a UserControl containing some controls of which one is a
ComboBox.
All the InternalControls are Private and some are allowed to be
accessed through Public Methods.
One of the things I would like to do (of course) is set the
DataBindings.
Something clearly is wrong with the approach I use, because I can't
set the Properties in the Designer, the DataSource-Property appears like
'ReadOnly', there is no "dropdown" with the list from
System.Windows.Forms.ListControl

Dim _DataSource As Object
<EditorBrowsable(EditorBrowsableState.Always), _
Browsable(True), _
Description("The data source for the dropdown list."),
Category("Data")> _
Public Overridable Property DataSource() As Object
Get
Return _DataSource
End Get
Set(ByVal Value As Object)
_DataSource = Value
Me.Internal_UiComboBox.DataSource = Value
End Set
End Property

Dim _DisplayMember As String
<EditorBrowsable(EditorBrowsableState.Always), _
Browsable(True), _
Description("Specifies the property of the data source whose content
you want to display."), Category("Data")> _
Public Overridable Property DisplayMember() As String
Get
Return _DisplayMember
End Get
Set(ByVal Value As String)
_DisplayMember = Value
Me.Internal_UiComboBox.DisplayMember = Value
End Set
End Property


In the Advanced Wizard I can Set a Value, only, I can't set a
DataSet, only a DataSet.DataTable.DataColumn.

What is wrong with my approach?

TIA,

Michael
 
M

Michael Maes

By the way: I guess "ExtendedDataGrid.Utilities.DataMemberFieldEditor" is a
Custom EditorType?
 
E

Ernest Morariu

By the way: I guess "ExtendedDataGrid.Utilities.DataMemberFieldEditor" is
a
Custom EditorType?

Yes, it is a cutom editor. Sorry, I haven't noticed this.
In your case , you don't need to use a custom DataMemberFieldEditor. You
can use the one of the framework, as you already did.


I think you forgot this attribute:
RefreshProperties(RefreshProperties.All)
If is set the DataBindings like:
DataSource = DataSet
DataMember = DataTable


I think this is not a valid code. Usualy, the DataMember property is of type
string, so you cannot set a DataTable object to a property of type String.

You make a little confusion. You are talking about DisplayMember,
ValueMember (that are to be found in ListBox, Combobox controls) and then
you are talking about DataMember that is to be found only in a DataGrid.

Usually the DataMember property is used to specify the table name inside the
DataSet.
DataSource=myDataSetObject
DataMember= "myTable"

Ernest



Yes it is
 
M

Michael Maes

I think you forgot this attribute:
RefreshProperties(RefreshProperties.All)

I already tried that. Didn't work :-(
You make a little confusion. You are talking about DisplayMember,
ValueMember (that are to be found in ListBox, Combobox controls) and then
you are talking about DataMember that is to be found only in a DataGrid.

I'm sorry. I wasn't talking about System.Windows.Forms.ComboBox but a
"Custom ComboBox" (more like a 'Dropdown DataGrid'). The issue is passing
Databinding to a UserControl, so I forgot about the Custom ComboBox.
Usually the DataMember property is used to specify the table name inside the
DataSet.
DataSource=myDataSetObject
DataMember= "myTable"

That is exactly what I need.

I'm going to look at the Sample Jared pointed out.

Regards,

Michael
 
P

Peter Huang

Hi Michael,

Based on my test with your code, if we set the dataset.datatable in the
datasource, in the displaymember and the valuemember we can only choose the
column from the table we specified according to my understanding of the
picture that you post and named it as wanted. Is that what you want?

So my understanding is that you want when you set the datasouce to dataset,
and datamember to datatable, then the displaymember and value member should
be in the datatable specifed before.

It seems that you wants the similar behavor with ListBox in .net frame.
If we set the display member and value member in different table a
exception will be thrown.
So I think we can do the similar thing in the usercontrol, and can write a
if--else statement in the Set of DisplayMember and ValueMember to judge the
binding context, if they are not in the table of datamember, then we can
throw the argument exception as the listbox do, so that we can avoid the
user make displaymember and valuemember bind to two different tables.

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 
M

Michael Maes

Hi Peter,

Thanks for your reply.
I'm sorry for my late reply, but we have experienced a major system-crash so
I've been "out-of-order" for a while.

Your understandings of my requirements are completely right.
Adding a conditional check to the Get is not a problem at all, only I would
prefer that only the columns of the Table selected in de DataMember are
shown.
I think I will have to write my own custom Editor for that, so I guess I'll
be looking in that direction now.

Regards,

Michael
 
E

Ernest Morariu

I'm sorry. I wasn't talking about System.Windows.Forms.ComboBox but a
"Custom ComboBox" (more like a 'Dropdown DataGrid'). The issue is passing
Databinding to a UserControl, so I forgot about the Custom ComboBox.

I made many controls this way and they works properly.

Tell me what is the name of your property representing the data source at
the UserControl level.

Ernest
 
P

Peter Huang

Hi Michael,

So far I think we would better do the job as the listbox do in the .net
framework. And I think It is hard to implement a customized Editor.

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top