Data Type

S

shapper

Hello,

I need to use a data type where I can add items, sort them
alphabetically and then use it as a datasource or bind it to a
DropDownList.

Could someone please tell me which data type should I use?

I was trying a ArrayList but I am not able to use it as my DropDownList
datasource.

Thanks,

Miguel
 
S

shapper

Nate said:
The easiest thing to use would be a datatable. For a more elegant solution,
you'll need your own custom entity that implements IComparable (for sorting):

public class MyEntity : IComparable
{
private ... (private fields)
public string MyProperty
{
get { return _myproperty; }
}
.. more public properties
public int CompareTo(object obj)
{
MyEntity temp = obj as MyEntity;
if (temp != null)
return this.MyProperty.CompareTo(temp.MyProperty);
}
}


Then you can add these little bastards to a List<>:

System.Collections.Generic.List<MyEntity> myEntities = new
System.Collections.Generic.List<MyEntity>();

myEntities.Add(new MyEntity(....));
// add however many
myEntities.Sort();


ddl.DataTextField = "WhatEverPublicPropertyYouWantedToShowUpAsText";
ddl.DataValueField = "WhatEverPublicPropertyYouWantedAsAnInternalValue";
ddl.DataSource = myEntities;
ddl.DataBind();

viola...

Thanks,

That is a really good solution.

Thanks once again,
Miguel
 

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
474,431
Messages
2,571,679
Members
48,796
Latest member
Greg L.

Latest Threads

Top