Binding a data source to a collection of objects

C

Christoph Boget

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
 
S

Scott Allen

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,
 
T

Teemu Keiski

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,
 
C

Christoph Boget

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
 
T

Teemu Keiski

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.
 
D

Dgates

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.";
}
}
}
 
C

Christoph

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
 
G

Guest

A few questions:

1. So if I'm binding a dropdown list to my DataSet (which has been populated
from a database), to set the .DataTextField to a combination of two columns
from the DataSet table I need to either create properties to do the string
manipulation or do the string combining in the sql query?


2. If I use an ArrayList as my data source, how do I construct the accessor
methods? That is, forget about properties for a moment, if I wanted the 5th
item in the ArrayList, I would write the method:

public string getTheField(int index) { ... } - where index would equal 4.

How is this written into the property? Does reflection take care of getting
the appropriate value?


3. Expanding on that, say I have an ArrayList of ArrayLists where the inner
ArrayLists correspond to rows in a database resultset. So, here the 5th field
in the outer ArrayList gets the 5th deeper ArrayList which is the 5th row of
data. Can the methods/properties be constructed to do this? If so, cold you
provide a brief example of the syntax. It would clear up some questions I
have. (I'm translating some old JSP code.)

Thank you for your time.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top