473,396 Members | 2,039 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,396 software developers and data experts.

Binding a data source to a collection of objects

Consider the following:

class MyClass {
public string Var1;
public string Var 2;
}

class MyClassList : System.Collections.CollectionBase {
// requisite access methods
}

Now, if I have a drop down box, can I define the the
datasource to be an instatiation of 'MyClassList' filled
with 'MyClass' objects? And set the 'DataTextField'
and 'DataValueField' properties to the 'Var1' and 'Var2'
properties of the 'MyClass' objects? If so, how?
Because I tried it and it didn't work. I think it's because
it doesn't know how to implicitely access the 'Var1' and
'Var2' properties, but that's just my guess.

thnx,
Christoph
Nov 18 '05 #1
6 1200
Hi,

data source resolving for the DDL expects that the member of the
collection/list item you look for the value is actually a property,
literally. Only public field doesn't qualify here. So if you put your code
like this:

*************
//Collection item
public class MyClass
{
public MyClass(string var1,string var2)
{
this._Var1 = var1;
this._Var2 = var2;
}

private string _Var1;
private string _Var2;

public string Var1
{
get
{
return _Var1;
}
}

public string Var2
{
get
{
return _Var2;
}
}

}

//Collection, brief for clarity
public class MyClassList : System.Collections.CollectionBase
{
public int Add(MyClass obj)
{
return this.List.Add(obj);
}
}

//Usage example:
MyClassList list=new MyClassList();
list.Add(new MyClass("The first","The second"));
list.Add(new MyClass("1st","2nd"));

DropDownList1.DataSource =list;
DropDownList1.DataValueField ="Var2";
DropDownList1.DataValueField ="Var1";
DropDownList1.DataBind();

*************

You'd have a working example.

Thanks,

--
Teemu Keiski
MCP, Microsoft MVP (ASP.NET), AspInsiders member
ASP.NET Forum Moderator, AspAlliance Columnist
http://blogs.aspadvice.com/joteke

"Christoph Boget" <jc*****@yahoo.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
Consider the following:

class MyClass {
public string Var1;
public string Var 2;
}

class MyClassList : System.Collections.CollectionBase {
// requisite access methods
}

Now, if I have a drop down box, can I define the the
datasource to be an instatiation of 'MyClassList' filled
with 'MyClass' objects? And set the 'DataTextField'
and 'DataValueField' properties to the 'Var1' and 'Var2'
properties of the 'MyClass' objects? If so, how?
Because I tried it and it didn't work. I think it's because
it doesn't know how to implicitely access the 'Var1' and
'Var2' properties, but that's just my guess.

thnx,
Christoph

Nov 18 '05 #2
Hi Christoph:

To do this, make sure you expose a public property (not just a field).

public string Var1
{
get { return _var1; }
}

Then you can set DataTextField = "Var1".

HTH,

--
Scott
http://www.OdeToCode.com

On Thu, 12 Aug 2004 12:40:34 -0500, "Christoph Boget"
<jc*****@yahoo.com> wrote:
Consider the following:

class MyClass {
public string Var1;
public string Var 2;
}

class MyClassList : System.Collections.CollectionBase {
// requisite access methods
}

Now, if I have a drop down box, can I define the the
datasource to be an instatiation of 'MyClassList' filled
with 'MyClass' objects? And set the 'DataTextField'
and 'DataValueField' properties to the 'Var1' and 'Var2'
properties of the 'MyClass' objects? If so, how?
Because I tried it and it didn't work. I think it's because
it doesn't know how to implicitely access the 'Var1' and
'Var2' properties, but that's just my guess.

thnx,
Christoph


Nov 18 '05 #3
> data source resolving for the DDL expects that the member of the
collection/list item you look for the value is actually a property,
literally. Only public field doesn't qualify here. So if you put your code
like this:


What's the difference between have a public field and having
a upblic accessor field? IOW, what's the difference between
this:

public string Var1;

and this:

private string _Var1;
public string Var1 { get{ return _Var1; }}

In both cases, 'Var1' is a public variable.

thnx,
Christoph
Nov 18 '05 #4
Yeah,

what comes to the object's interface they are similar but of course property
is different from normal field in that in can contain logic in get/set
accessors before the underlying value is accessed.

As I said, it is all up to the data source resolving done for the
DropDownList. The value of the given property is checked by using reflection
and it only looks for properties (it uses DataBinder.GetPropertyValue). In
the metadata which reflection uses for lookup, public fields and proeprties
are different things.

--
Teemu Keiski
MCP, Microsoft MVP (ASP.NET), AspInsiders member
ASP.NET Forum Moderator, AspAlliance Columnist
http://blogs.aspadvice.com/joteke
"Christoph Boget" <jc*****@yahoo.com> wrote in message
news:eS**************@TK2MSFTNGP09.phx.gbl...
data source resolving for the DDL expects that the member of the
collection/list item you look for the value is actually a property,
literally. Only public field doesn't qualify here. So if you put your code like this:


What's the difference between have a public field and having
a upblic accessor field? IOW, what's the difference between
this:

public string Var1;

and this:

private string _Var1;
public string Var1 { get{ return _Var1; }}

In both cases, 'Var1' is a public variable.

thnx,
Christoph

Nov 18 '05 #5
On Thu, 12 Aug 2004 13:36:26 -0500, "Christoph Boget"
<jc*****@yahoo.com> wrote:
data source resolving for the DDL expects that the member of the
collection/list item you look for the value is actually a property,
literally. Only public field doesn't qualify here. So if you put your code
like this:


What's the difference between have a public field and having
a public accessor field? IOW, what's the difference between
this:

public string Var1;

and this:

private string _Var1;
public string Var1 { get{ return _Var1; }}

In both cases, 'Var1' is a public variable.

If you're wondering why use the alternate syntax (ignoring the DDL
needs for now)... I was just reading about this in a book! :-)

You can do cool tricks with "get" that you just can't do with a public
string!
YOU MENTIONED THIS:

private string _Var1;
public string Var1
{
get
{
return _Var1;
}
}

BUT WHAT ABOUT THIS:

private string _Var1;
public string Var1
{
get
{
numTimesVarHasBeenAccessed++; // PRETTY COOL, EH?
return _Var1;
}
}

OR THIS:

private string _Var1;
public string Var1
{
get
{
if ( _Var1 == "" )
{
return _Var1;
}
else
{
return "Sorry, no var available.";
}
}
}
Nov 18 '05 #6
> If you're wondering why use the alternate syntax (ignoring the DDL
needs for now)... I was just reading about this in a book! :-)


I know the benefits of using accessors. I was just curious what the
difference was between the two such that one would work for what
I needed it for while the other would not.

thnx,
Christoph
Nov 18 '05 #7

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

Similar topics

0
by: Ann Morris | last post by:
INTRODUCTION One of the most powerful aspects of .NET and Windows Forms is data binding. Data binding is the process of associating user interface (UI) elements with a data source to generate a...
5
by: K | last post by:
I created a collection which is derived from ArrayList and implements IBindingList and ITypedList. Then I bound the DataSource of a data grid into the collection. It could show up the data but...
1
by: Antero | last post by:
I'm desperate! :-/. I'm desperately trying to bind to objects together to share same data and to display it in a windows forms. I'm trying to bind an custom object to custom control property. I...
1
by: matty.hall | last post by:
There's a lot of information out there about data-binding UI objects (i.e. derived from Control) to non-UI custom business objects. Is it possible to do the same without any UI being involved at...
1
by: Dot net work | last post by:
Hello. I have an interesting data binding scenario: I have a repeater control. It repeats a typical custom web user control. I also have a collection object, and each collection element...
3
by: bbernieb | last post by:
Hi, All, Is it possible to access a variable inside of a data binding, without the variable being out of scope? (Note: On the DataBinder line, I get an error message that says "Name 'i' is...
0
by: Stephajn Craig | last post by:
I've built a customized collection class that has strongly typed objects in it. I have a collection class called LogEntryCollection with LogEntry objects in it. Each LogEntry object has certain...
9
by: Jaybuffet | last post by:
my aspx has something like this <asp:Repeater id="Repeater1" runat="server"> <ItemTemplate> <mycontrol:ctl id="ctlId" obj='<%# Container.DataItem %>' showItem="true"/> </ItemTemplate>...
2
by: Paul | last post by:
Hello All, I am teaching myself C#. The book I am using has the following example using a BindingSource object. Here is the code. NorthwindDataSetTableAdapters.ProductsTableAdapter productsTA...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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...
0
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
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
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,...

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.