473,386 Members | 1,715 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

How to create a class I can bind a list (or other control which works with collections) to

bz
Hi,

I create a class that retrieves a number of records from database.
Internally, the class store the returned data in a datatable /
datareader. I would like to bind a listbox to that class (to the
records from this datatable), using data binding wizard and to be able
to select bind to object
How should I make this class to be able to accomplish this? Do I have
to define it to implement some interface? Which one? I want to bind
DisplayMember to a certain field from the datatable

Thank you

Oct 8 '07 #1
5 1539
You can implement ITypedList in your collection class.

--
Sheng Jiang
Microsoft MVP in VC++
"bz" <bz*****@gmail.comwrote in message
news:11**********************@v3g2000hsg.googlegro ups.com...
Hi,

I create a class that retrieves a number of records from database.
Internally, the class store the returned data in a datatable /
datareader. I would like to bind a listbox to that class (to the
records from this datatable), using data binding wizard and to be able
to select bind to object
How should I make this class to be able to accomplish this? Do I have
to define it to implement some interface? Which one? I want to bind
DisplayMember to a certain field from the datatable

Thank you

Oct 8 '07 #2
Internally, the class store the returned data in a datatable /
datareader.
Note that data-readers are generally firehose (one-way) iterators, so
don't really "store" anything. Almost any binding work will involve
reading over the data many times, so datareader probably won't work.
Of course, if you are considering DataTable under the covers, why not
just use DataTable all the way? It will support everything you need
and more. What is your wrapper providing?
You can implement ITypedList in your collection class.
For completeness: for runtime binding, ITypedList is generally
overkill. The binding code checks for a range of things, but in most
cases simply implementing IList and providing a typed indexer
(SomeType this[int index]) is enough; the standard binding code will
assume that this is a list of items, typed by the indexer (i.e.
SomeType). This means that in 2.0, List<Twill do everything you
need. If you want better support for property notification,
BindingList<Twill do what you need.

As for design-time (IDE) binding - your guess is as good as mine! Due
to the instability in the IDE (and too many lost bindings) I gave up
on design-time bindings a long time ago.

Marc
Oct 8 '07 #3
bz
On 8 Oct, 22:28, Marc Gravell <marc.grav...@gmail.comwrote:
Internally, the class store the returned data in a datatable /
datareader.

Note that data-readers are generally firehose (one-way) iterators, so
don't really "store" anything. Almost any binding work will involve
reading over the data many times, so datareader probably won't work.
Of course, if you are considering DataTable under the covers, why not
just use DataTable all the way? It will support everything you need
and more. What is your wrapper providing?
My class looks like this:

class MyCollection
{
protected DataTable prvMyTable;

public MyCollection(string SomeCriteria)
{ // here I use SQLHelper to run a sql or stored proc which
returns a dataset
myDS = SQLHelper(....)
prvMyTable = myDS.Tables[0];
}
// then I expose the prv through a property
public DataTable Objects {
get {
Check.Ensure(prvMyTable != null, "Objects collection
was not retrieved ", new ArgumentNullException("prvMyTable"));
return prvMyTable;
}
}

And I access the fields through Columns collection

How should I implement IList? I saw DataTable implements
IListCollection interface.
Should I implement this interface in my wrapper? If so, how, since I
tried, but it seems DataTable does not have the GetList method, which
is required for IListCollection interface.

Thanks


>
You can implement ITypedList in your collection class.

For completeness: for runtime binding, ITypedList is generally
overkill. The binding code checks for a range of things, but in most
cases simply implementing IList and providing a typed indexer
(SomeType this[int index]) is enough; the standard binding code will
assume that this is a list of items, typed by the indexer (i.e.
SomeType). This means that in 2.0, List<Twill do everything you
need. If you want better support for property notification,
BindingList<Twill do what you need.

As for design-time (IDE) binding - your guess is as good as mine! Due
to the instability in the IDE (and too many lost bindings) I gave up
on design-time bindings a long time ago.

Marc

Oct 8 '07 #4
jut use DataTable.DefaultView or create another view if you want. DataView
implements a wide range of data binding interfaces.

--
Sheng Jiang
Microsoft MVP in VC++
"bz" <bz*****@gmail.comwrote in message
news:11**********************@d55g2000hsg.googlegr oups.com...
On 8 Oct, 22:28, Marc Gravell <marc.grav...@gmail.comwrote:
Internally, the class store the returned data in a datatable /
datareader.
Note that data-readers are generally firehose (one-way) iterators, so
don't really "store" anything. Almost any binding work will involve
reading over the data many times, so datareader probably won't work.
Of course, if you are considering DataTable under the covers, why not
just use DataTable all the way? It will support everything you need
and more. What is your wrapper providing?

My class looks like this:

class MyCollection
{
protected DataTable prvMyTable;

public MyCollection(string SomeCriteria)
{ // here I use SQLHelper to run a sql or stored proc which
returns a dataset
myDS = SQLHelper(....)
prvMyTable = myDS.Tables[0];
}
// then I expose the prv through a property
public DataTable Objects {
get {
Check.Ensure(prvMyTable != null, "Objects collection
was not retrieved ", new ArgumentNullException("prvMyTable"));
return prvMyTable;
}
}

And I access the fields through Columns collection

How should I implement IList? I saw DataTable implements
IListCollection interface.
Should I implement this interface in my wrapper? If so, how, since I
tried, but it seems DataTable does not have the GetList method, which
is required for IListCollection interface.

Thanks


You can implement ITypedList in your collection class.
For completeness: for runtime binding, ITypedList is generally
overkill. The binding code checks for a range of things, but in most
cases simply implementing IList and providing a typed indexer
(SomeType this[int index]) is enough; the standard binding code will
assume that this is a list of items, typed by the indexer (i.e.
SomeType). This means that in 2.0, List<Twill do everything you
need. If you want better support for property notification,
BindingList<Twill do what you need.

As for design-time (IDE) binding - your guess is as good as mine! Due
to the instability in the IDE (and too many lost bindings) I gave up
on design-time bindings a long time ago.

Marc


Oct 8 '07 #5
How should I implement IList? I saw DataTable implements
IListCollection interface.
Again - why not just work with DataTable (or DataView) directly? I'm
not sure what your wrapper is helping with... but to answer your
specific question, in this case, IListSource is the interface to
implement (which acts as an intermediary to IList) - which mimics
DataTable's behavior:

class MyCollection : IListSource {
bool IListSource.ContainsListCollection {
get {return ((IListSource)Objects).ContainsListCollection;}
}
IList IListSource.GetList() {
return ((IListSource)Objects).GetList();
}
...

Marc

Oct 9 '07 #6

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

Similar topics

0
by: olivier | last post by:
Please I need help to create (C#) a user control to emulate a flat dropdownlist that can be bind to datas
1
by: Joey Liang via DotNetMonster.com | last post by:
Hi all, I am new to ASP.NET, i have met some problems in binding data from a table of database to a dropdown list control. Anyone knows how to do it?if possible can rougly write the codes or any...
4
by: Ankit Aneja | last post by:
code of my aspx page <asp:RadioButtonList id="RadioButtonList1" runat="server"></asp:RadioButtonList> now i want to bind data from database Dim rsComm As SqlCommand Dim rsReader As...
1
by: Brian Henry | last post by:
is there any way to bind a list view to a datatable? rignt now i am filling it by looping through a datatables row's adding each row one by one to the listview... takes up a good amount of process...
4
by: emzyme20 | last post by:
Hi, I am trying to populate a list control that is bound to a data table but the display member needs to come from a different data table. I have two list controls in C#, one displaying...
2
by: Lakesider | last post by:
Hi NG, I want to use a List Control, that is able to display the following szenario: Customer A New York Revenue: 1200,- € Customer B
1
by: bsautner | last post by:
Hi, spent a while trying to figure this out myself and i seem to be missing something. I want to create a windows user control in vb.net 8 that i can then use in office applications like...
4
by: John | last post by:
Hello, I was wondering if it was possibly to bind a control to a dataset.haschanges property. The reason I want to do this is so that a little warning shows up on the form saying that the...
1
by: Scott H | last post by:
I'm writing a chess game reviewer for Yahoo! players. This will be my first Visual C++ application. Right now the list box containing the moves in a history file looks like this: ___________...
0
by: AngZangGui | last post by:
I am using MFC in Visual Studio 2005. I had added a list control in my dialog and set the view property as "Icon". I declared the ImageList in the OnInitDialog of the dialog where the listview is...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.