473,508 Members | 2,008 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

DataBind property of class which derives from CollectionBase

Joe
I want to bind to the property Caption but can't. I get the exception:
"Cannot bind to property or column Caption on DataSource.
Parameter name: dataMember"

public class Test : CollectionBase
{
private string caption;

public string Caption
{
get
{
return caption;
}
set
{
caption = value;
}
}
}

Is there anyway to do this when a class derives from CollectionBase?

Thanks,
Joe
Aug 12 '06 #1
2 2118
How are you setting the datasource? BindingSource, for instance, will
accept a single object *or* an object that implements IList to mean a
set of objects. I suspect you are trying to use the first (single
object) case; since CollectionBase : IBindingSource, it could be
thinking you mean to give it a set of objects, and thus be a level out
(if you see what I mean). Try placing your object into a List<T>, and
set *that* (list) as the datasource.

Marc

Aug 12 '06 #2
Can reproduce on demand; see fix below: (remove marked line and
uncomment remaining 3):

class Test : System.Collections.CollectionBase {
public string Caption { get { return "Working"; } }
}
class Program {
static void Main() {
Test obj = new Test();
using (Form f = new Form())
using(BindingSource bs = new BindingSource()) {
bs.DataSource = typeof(Test);
f.DataBindings.Add("Text", bs, "Caption");
bs.DataSource = obj; // breaks
//List<TestallData = new List<Test>();
//allData.Add(obj);
//bs.DataSource = allData;
f.ShowDialog();
}
}
}

Marc

Aug 12 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
4361
by: Marc L'Ecuyer | last post by:
I want to implement a collection property inside another (in a control derived class) but I have some persistence problems. There is an example of my ColumnItem class ...
3
2520
by: Marc L'Ecuyer | last post by:
Hi, I have a collection class derived from CollectionBase. This collection can add items of my class MyItem. In my class MyItem, I have a Selected property. When this property is set to true, I...
3
3414
by: Marina | last post by:
Hi, I have a class that inherits from CollectionBase, and I'm trying to override its Count property. As per the documentation, this property is overridable - so I should be able to do this. So...
4
3680
by: Doug | last post by:
I'm trying to create a user control that would have a ListDictionary property. When trying to use the property and set values for this collection, the dialog box that appears has everything...
4
2471
by: Roberto Sartori | last post by:
Hi. I'd want to know if it is possible in C # to declare one property analogous to following (written in VB): Property PropertyName (ByVal Index As Integer) As Object Get Return...
6
7931
by: GingerNinja | last post by:
Hi Everybody, its about my 4th day on C# and all seems to be going smoothly however I've started to get into Datagrids and in particular binding data sources to them. In the documentation it says I...
0
1422
by: Jordan Bowness | last post by:
I make a similar post in another newsgroup, but this example is simplified somewhat. I have a component (cmpMyComponent) with 2 properties. The 1st property is a string value (Description) and...
3
4738
by: R.A.F. | last post by:
Hi, I have a custom control in which i have a property based on CollectionBase class. like the following one : //------- XGrid.cs file --------------// public ColumnsCollection Columns {
2
1515
by: C. Herzog | last post by:
Hi! I want to create a huge amount of classes. All classes have in common that the change of a property should fire an Change-Event and maybe do other things. I don't want to write thousand...
0
7127
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7331
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
7054
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7501
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5633
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5056
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3204
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3188
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
424
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.