Groups | Blog | Home
all groups > dotnet windows forms databinding > july 2006 >

dotnet windows forms databinding : How to build own datasource component which works with VS designer


Rolf Welskes
7/24/2006 7:41:12 PM
Hello,

I have build a componente comp
In this I have 3 components comp01, comp02, comp03.
With Properties for this Comp01, Comp02, Comp03.
comp01, comp02, comp03 implement IBindingList and are the lists I want to
bind to.

comp implements IListSource.

IListSource.ContainsListCollections return true.

I do not know what to return with IListSource.GetList ???

If I us comp in a form an try to bind to a listbox the following works:
ListBox.DataSource can be bound by designer at for example comp.comp01 .
But if I bind Listbox to comp and want to see comp01, comp02, comp03
at for example DisplayMember in the Designer combox is nothing.

So I think the problem is what to return with IListSource.GetList ?

Thank You for an answer.
Rolf

v-lliu NO[at]SPAM online.microsoft.com
7/25/2006 12:00:00 AM
Hi Rolf,

Thank you for posting.

[quoted text, click to view]
for example DisplayMember in the Designer combox is nothing. So I think the
problem is what to return with IListSource.GetList ?

I am sorry but I don't think you could see comp01, comp02 and omp03 at
DisplayMember in the Properties window no matter what to return with
IListSource.GetList.

IListSource.GetList method returns a collection which implements IList. If
the type of the objects in this collection is definite at design time, VS
will recognize the type and enumerate all its public properties at
DisplayMember in the Properties window.

Here is an example. I set up Windows application and place a ListBox on a
form. I create a component called MyListSource in the project. Below is the
code of MyListSource.

using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;

class MyListSource:Component,IListSource
{
System.Collections.IList IListSource.GetList()
{
List<Employee> employees = new List<Employee>();
return employees;
}
}

public class Employee
{
private string _id;
private string _name;
private Decimal parkingId;

public string ID
{
get { return _id; }
set { _id = value;}
}
public string Name
{
get { return _name; }
set { _name = value; }
}
public Decimal ParkingID
{
get { return parkingId; }
set { parkingId = value;}
}
}

Compile the project. Drag&drop MyListSource from toolbox onto a form to add
an instance of MyListSource(e.g myListSource1) in the form. Select the
ListBox and set its DataSource property to myListSource1. Then VS will
display ID,Name and ParkingID at DisplayMember in the Properties window.

Could you tell me why you want to show comp1, comp2 and comp3 at
DisplayMember in the Properties window?

Hope it helps.


Sincerely,
Linda Liu
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
v-lliu NO[at]SPAM online.microsoft.com
7/28/2006 1:40:41 PM
Hello Rolf,

I am closely monitoring the newsgroup and I am contacting you to check the
issue status.

If you have anything unclear, please feel free to post in the newsgroup and
we will follow up.

Thank you for using our MSDN Managed Newsgroup Support Service!



Sincerely,
Linda Liu
Microsoft Online Community Support

Rolf Welskes
7/30/2006 12:00:00 AM
Hello,
thank you,
but your sample is the simple case.

It's right, comp has the components comp01, comp02, comp03.
And I want to bind for example to comp.comp02.Street.

What I want is the same as a dataset can.

For example comp is DS, comp01 is Table01, comp02 is Table02, etc.

Now in a form you have two ways:
From the listbox in visual studio designer set
DataSource = ds.Table02;
DisplayMember (or ValueMember) = Street; here you see this in a list in
the designer.

This also works with comp, comp01, comp02, comp03.

But the problem is here:
With DataSet you can:

in visual studio designer bind:
the listbox

DataSource = ds;
DisplayMember = Table02.Street.

For DS you see after you have set DataSource = ds a list of the sub
components Table1, Table2, Table03.
Each Table has a + so you can expand and set to for example Street.

With comp, comp01, comp02, comp03, after you have set
DataSource = comp;
You do not see comp01, comp02, comp03 as a list in Displaymember (if it
must have + (tree), to select the property, clearly).

Thank You.
Rolf

v-lliu NO[at]SPAM online.microsoft.com
8/1/2006 11:46:44 AM
Hi Rolf,

Thank you for your update.

I think you should return the comp itself in the GetList method to expose
the child elements comp01, comp01 and comp03. This requires that you have
written public properties for comp01, comp02 and comp03. The following is a
sample.

IList IListSource.GetList()
{
List<comp> ble = new List<comp>();
ble.Add(this);
return ble;
}

Thus, when you add an instance of the comp(i.e comp1) onto the form, you
could set the ListBox's DataSource to comp1 in the Properties window and in
DisplayMember node you could see comp01, comp02 and comp03 are listed in
the combobox.

Hope this helps.
If you have anything unclear, please feel free to let me know.


Sincerely,
Linda Liu
Microsoft Online Community Support


v-lliu NO[at]SPAM online.microsoft.com
8/2/2006 12:00:00 AM
Hi Rolf,

If you have written public properties for the fields Tst01, Tst02 ,etc in
the type MyTstCls, these properties will appear under the Comp02 node in
the ValueMember listbox.

Hope this helps.
If you have anything unclear, please feel free to let me know.


Sincerely,
Linda Liu
Microsoft Online Community Support
Rolf Welskes
8/2/2006 3:02:08 AM
Hello,
thank you this works.
But only until the next problem.

Now I can see the components comp01, comp02, comp03 in ValueMember of the
listbox (or displayMember, is the same in this case).

But, if you set DataSource = ds;
you get in ValueMember not only the components which are tables, Table01,
Table02, etc,
but also at any of this you find a + and can expand to see the properties of
the table-row.

In my case comp01, comp02, etc. also are collections of a type MyTstCls
and MyTstCls has the properties, string Tst01, string Tst02, string Tst02.
So I should see under ValueMember:

+Comp01
+Comp02
+Comp03

and if I expand I should see for example

-Comp02
Tst01
Tst02
Tst03

And this does not work.

The VS designer does not see that for example comp01 is a collection of
MyTstCls-Objekts which have the properties Tst01, TSt02, Tst03.

I this were there, I had the same behave as that of a DataSet what is the
intention.

Thank you for your work.
Rolf





"Linda Liu [MSFT]" <v-lliu@online.microsoft.com> schrieb im Newsbeitrag
news:8LopxAWtGHA.3920@TK2MSFTNGXA01.phx.gbl...
[quoted text, click to view]

Rolf Welskes
8/3/2006 10:28:47 PM
Hello,
I have also thought so, but the properties are not there under valueMember.

Maybe the reason is the implementation of the components comp01, comp02,
comp03 as lists.

I have tried to implement only IList does not work.
I have tried to implement IBindingList, does not work.
allways I return MyTstCls-objects.


What must be in the components comp01, comp02, so it is a list from
MyTstCls-objects,
so that VS designer sees this, so that I get under valueMember in a listbox

comp01
Tst01
Tst02
Tst03

Tst01, Tst02, Tst03 are public properties of the MyTstCls.

Thank you.
Rolf

v-lliu NO[at]SPAM online.microsoft.com
8/4/2006 12:00:00 AM
Hi Rolf,

In my test project, the comp01, comp02 and comp03 are of type SubComponent,
which is inherited from CollectionBase and implements IBindingList.

I have implemented index property in the SubComponent. Below is the index
property.

// Employee is the type of objects this collection contains
public Employee this[int index]
{
get
{
return (Employee)(List[index]);
}
set
{
List[index] = value;
}
}

If I comment this property out, I couldn't see the properties in the
Employee under comp01, comp02, or comp03 in the DisplayMember or
ValueMember comboboxes.

I think you should add a similar property in your class.

Hope this helps.
If you have anything unclear, please feel free to let me know.


Sincerely,
Linda Liu
Microsoft Online Community Support

Rolf Welskes
8/6/2006 6:19:38 PM
Hello,
thank you.
But it does not work.

I think we are very near that it works, but there is one thing what is
wrong.

I have listed here the three classes (the relevant parts), what is wrong?


Comp - the main-components


[Serializable]
public class Comp : System.ComponentModel.Component, IListSource,
ISerializable
{
.....
.....



public IList GetList()
{
TstComp01[] cl = new TstComp01[]{this};
return cl;
}

public bool ContainsListCollection
{
get
{
return true;
}
}


[System.ComponentModel.DesignerSerializationVisibilityAttribute(System.ComponentModel.DesignerSerializationVisibility.Content)]
public TestObjsList TestObjsList
{
get
{
return testObjsList;
}
}


}

//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

//the subcomponent is the list

public class TestObjsList : CollectionBase, IBindingList
{
private ListChangedEventArgs resetEvent = new
ListChangedEventArgs(ListChangedType.Reset, -1);
public event ListChangedEventHandler ListChanged;

public TestObjsList()
{
}

....

public TestObj this[int index]
{
get
{
return (TestObj)(List[index]);
}
set
{
List[index] = value;
}
}

public int Add (TestObj value)
{
return List.Add(value);
}

....
}


//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
//TestObj, the object in the list.

public class TestObj : IEditableObject
{
...
...

public string ID
{
get
{
return this.custData.id;
}
}

public string FirstName
{
get
{
return this.custData.firstName;
}
set
{
this.custData.firstName = value;
OnTestObjChanged() ;

}
}

public string LastName
{
get
{
return this.custData.lastName;
}
set
{
this.custData.lastName = value;
OnTestObjChanged() ;
}
}

public double Sales
{
get
{
return this.custData.sales;
}
set
{
this.custData.sales = value;
OnTestObjChanged() ;
}
}
}

So if I set in a listbob:
DataSource = Comp;

I should see in the valueMember the list
TestObjList
ID
LastName
Sales

But I only see TestObjList, not the properties.

Thank You
Rolf





AddThis Social Bookmark Button