473,762 Members | 8,011 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1559
You can implement ITypedList in your collection class.

--
Sheng Jiang
Microsoft MVP in VC++
"bz" <bz*****@gmail. comwrote in message
news:11******** **************@ v3g2000hsg.goog legroups.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<Twi ll 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...@g mail.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(st ring 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(pr vMyTable != null, "Objects collection
was not retrieved ", new ArgumentNullExc eption("prvMyTa ble"));
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<Twi ll 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.Defau ltView 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.goo glegroups.com.. .
On 8 Oct, 22:28, Marc Gravell <marc.grav...@g mail.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(st ring 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(pr vMyTable != null, "Objects collection
was not retrieved ", new ArgumentNullExc eption("prvMyTa ble"));
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<Twi ll 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.Con tainsListCollec tion {
get {return ((IListSource)O bjects).Contain sListCollection ;}
}
IList IListSource.Get List() {
return ((IListSource)O bjects).GetList ();
}
...

Marc

Oct 9 '07 #6

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

Similar topics

0
1216
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
6070
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 tutorials regarding this...Thanx in advance... From, Joey --
4
1318
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 SqlDataReader
1
1094
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 time... is there any way around it? thanks
4
2056
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 available categories and one displaying selected categories. In the database these are represented in two tables, the available category table has an ID and a Name. The other table is a category links table that has an ID, Category_ID and a Book_ID.
2
2477
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
1715
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 powerpoint or visio. So in VS 2005 i create a new project and select "Windows Control" create my dll and it's functionality. I'd like to then launch visio and select "INSERT" then CONTROLS and select my new control from the list provided (cannot browse...
4
1308
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 record has not been saved. Thank you heaps for anyones thoughts John Sheppard
1
3266
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: ___________ |Start |1. W e2-e4 |2. B e7-e5 |3. W g1-f3 |4. B b8-c6
0
1785
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 displayed. The dialog is supposed to open when I clicked a button from the main dialog. However, I keep on getting this problem which I guess is due to the hWnd = 0x00000000. m_ListView {CListCtrl hWnd=0x00000000} CListCtrl Part of my code:...
0
9554
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10137
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9989
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9927
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8814
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6640
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5405
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
3510
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2788
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.