Attention: We are retiring the ASP.NET Community Blogs. Learn more >

Infragistics WinGrid Sort

I was trying to sort an Infragistics WinGrid automatically after each row insert or update by the user so in the AfterRowUpdate event, I added the following code:

grd.DisplayLayout.Bands[0].Columns["MyColumn"].SortIndicator = Infragistics.Win.UltraWinGrid.SortIndicator.Ascending;
grd.DisplayLayout.Bands[0].Columns["MyColumn"].Band.SortedColumns.RefreshSort(true);
grd.Refresh();

This works fine except that the grid does an alphabetical sort even thought the column type is numeric.  Ex:
1
10
2
22
3

A quick search lead me to this Infragistics knowledge base article:
http://devcenter.infragistics.com/Support/KnowledgeBaseArticle.aspx?ArticleID=7695

Although not exactly what I wanted, I slightly changed to code to sort my numeric column.

public class srtComparer : IComparer
{
    public srtComparer()
    {}

    public int Compare(object x, object y)
    {
        UltraGridCell xCell = (UltraGridCell)x;
        UltraGridCell yCell = (UltraGridCell)y;
        return Decimal.Compare((Decimal)xCell.Row.Cells["MyColumn"].Value, (Decimal)yCell.Row.Cells["MyColumn"].Value);
        }
}

grd.DisplayLayout.Bands[0].Columns["MyColumn"].SortComparer = new srtComparer();
grd.DisplayLayout.Bands[0].Columns["MyColumn"].SortIndicator = Infragistics.Win.UltraWinGrid.SortIndicator.Ascending;
grd.DisplayLayout.Bands[0].Columns["MyColumn"].Band.SortedColumns.RefreshSort(true);
grd.Refresh();

Of course, the above class could be made more generic by having a column name or number as an argument.

I would have expected that the grid would sort correctly depending on the column type but I guess this behaviour gives more flexibility.

2 Comments

Comments have been disabled for this content.