Data Type

  • Thread starter Thread starter shapper
  • Start date Start date
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
 
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
 
Back
Top