Question

I am developing desktop application and that i am using Infragistic Controls version 12.1. Now in the UltraWinGrid there are columns like ID,Time,Name, Description etc.

Id  Time                  Name       Description
1   10.45 AM - 11:15 AM    ABC         ABC
2   09:15 AM - 09.45 AM    XYZ         XYZ
3   02:00 PM - 02.15 PM    ABDD        ABDD
4   03:00 PM - 03.45 PM    EFG         EFG
5   01:15 PM - 01:45 PM    BCD         EFG

Now if i click on header of any column except time column and try to sort it will works fine. but when i click on header of time column it will not working. it should be returns like following way.

    Id         Time               Name       Description

    2   09:15 AM - 09.45 AM    XYZ         XYZ
    1   10.45 AM - 11:15 AM    ABC         ABC   
    5   01:15 PM - 01:45 PM    BCD         EFG   
    3   02:00 PM - 02.15 PM    ABDD        ABDD
    4   03:00 PM - 03.45 PM    EFG         EFG

but some how it will not return correctly. And Time column is string field. So it is sorting according to string type field. but i want to sort time column as time field. So that is not working.

So Can anyone suggest me how to do this?

Thanks in advance,

Was it helpful?

Solution

The mentioned behavior is expected, because your [Time] column has data type of string. Maybe one possible approach to solve this task, could be if you are using IComparer interface. By this way you could implement your custom sorting behavior.

I have similar sample where I used IComparer interface. There I should sort integer by absolute values. You could use this sample as a starting point. More details about IComparer interface you could find at: http://help.infragistics.com/Help/NetAdvantage/WinForms/2013.1/CLR4.0/html/Infragistics4.Win.Misc.v13.1~Infragistics.Win.Misc.NavigationBarLocationsCollection~Sort(IComparer).html

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using Infragistics.Win.UltraWinGrid;

namespace UltraGridSortByABSValue
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            DataTable dt = new DataTable();
            dt.Columns.Add("A", typeof(string));
            dt.Columns.Add("B", typeof(double));
            dt.Columns.Add("C", typeof(double));

            dt.Rows.Add("Test 1", 10, 23.3);
            dt.Rows.Add("Test 2", 30, 23.4);
            dt.Rows.Add("Test 3", -20, 21.3);
            dt.Rows.Add("Test 4", -40, 12.3);
            dt.Rows.Add("Test 5", -50, -22.7);
            dt.Rows.Add("Test 6", 60, 22.3);
            dt.Rows.Add("Test 7", -70, 26.8);
            dt.Rows.Add("Test 8", 80, 13.3);
            dt.Rows.Add("Test 9", 90, 29.1);

            ultraGrid1.DataSource = dt;
            ultraGrid1.DisplayLayout.Bands[0].Columns["C"].Style = Infragistics.Win.UltraWinGrid.ColumnStyle.Currency;
            ultraGrid1.DisplayLayout.Bands[0].Columns["C"].Format = "$ #,##0.00;$ -#,##0.00; $ -";
            ultraGrid1.DisplayLayout.Bands[0].Columns["C"].CellAppearance.TextHAlign = Infragistics.Win.HAlign.Right;

        }

        private void ultraGrid1_AfterSortChange(object sender, BandEventArgs e)
        {
            ultraGrid1.DisplayLayout.Bands[0].Columns["B"].SortComparer = new SortComparer();
        }

        private void ultraGrid1_InitializeGroupByRow(object sender, InitializeGroupByRowEventArgs e)
        {
            //e.Row.Column.SortComparer = new SortComparer();
        }
    }

    public class SortComparer : IComparer
    {
        // Custom Sorting - Sort by ABS values
        int IComparer.Compare(object x, object y)
        {
            UltraGridCell cell1 = x as UltraGridCell;
            UltraGridCell cell2 = y as UltraGridCell;

            string string1 = Math.Abs((double)cell1.Value).ToString();
            string string2 = Math.Abs((double)cell2.Value).ToString();

            int ret;

            if (string1 == null)
            {
                ret = -1;
            }
            else
                if (string2 == null)
                {
                    ret = 1;
                }
                else
                {
                    ret = string.Compare(string1, string2, true, System.Globalization.CultureInfo.CurrentCulture);
                }
            return ret;
        }
    }

}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top