CategoryList sorted by name

Vote:
 

Is there a simple way to retrieve categories from a CategoryList object sorted by name?

By default, if I bind to a CategoryList the individual Category items are returned in Id order.

#20666
Jun 09, 2008 12:36
Vote:
 

protected DataTable GetSortedCategories(string parentCategoryName)
{
  DataTable returnValue = new DataTable(parentCategoryName);
  returnValue.Columns.Add(new DataColumn("Name", "".GetType()));
  returnValue.Columns.Add(new DataColumn("Id", 0.GetType()));

  Category cat = Category.Find(parentCategoryName);
  IList categories = cat.GetList();

  foreach (object obj in categories)
  {
    Category category = obj as Category;
    if (category != null)
    {
      DataRow dataRow = returnValue.NewRow();
      dataRow[0] = category.Name;
      dataRow[1] = category.ID;
      returnValue.Rows.Add(dataRow);
    }
  }
 
  returnValue.AcceptChanges();
  DataView dv = new DataView(returnValue);
  dv.Sort = "Name ASC";
 
  return dv.ToTable();
}

Keep in mind that I haven't done much testing of the code.

best regards
// Stefan

#22704
Aug 12, 2008 18:42