Tuesday, March 3, 2015

sort a collection by a property in C# and also make the dropdown list column types in a DevExpress grid allow for sorting

This...

<dx:GridViewDataComboBoxColumn FieldName="Group.Description" VisibleIndex="4"
      Caption="Group">
   <PropertiesComboBox ValueField="GroupId" DataSourceID="GroupSource"
         TextField="Description" ValueType="System.String"
         DropDownStyle="DropDown" />
</dx:GridViewDataComboBoxColumn>

 
 

...had to be revamped like so:

<dx:GridViewDataComboBoxColumn FieldName="Group.GroupId" VisibleIndex="4"
      Caption="Group">
   <PropertiesComboBox ValueField="GroupId" DataSourceID="GroupSource"
         TextField="Description" ValueType="System.
Int32"
         DropDownStyle="DropDown" />
</dx:GridViewDataComboBoxColumn>

 
 

... making the FieldName match the ValueField. My superior ended up writing something like so to sort the contents of the dropdown alphabetically without the uppercase Z getting sorted first before the lowercase a:

public List<Group> GetGroups()
{
   using (IMyRepository myRepository = GetRepository())
   {
      List<Group> groups = myRepository.GetAllGroups();
      groups.Sort((g1, g2) => String.Compare(g1.Description, g2.Description,
            StringComparison.CurrentCultureIgnoreCase));
      return groups;
   }
}

No comments:

Post a Comment