0

我正在开发桌面应用程序,并且我正在使用 Infragistic Controls 12.1 版。现在在 UltraWinGrid 中有 ID、时间、名称、描述等列。

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

现在,如果我单击除时间列以外的任何列的标题并尝试对其进行排序,它将正常工作。但是当我单击时间列的标题时,它将不起作用。它应该像以下方式返回。

    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

但有些如何它不会正确返回。时间列是字符串字段。所以它是根据字符串类型字段排序的。但我想将时间列排序为时间字段。所以这是行不通的。

那么谁能建议我如何做到这一点?

提前致谢,

4

1 回答 1

3

上述行为是预期的,因为您的 [Time] 列具有字符串数据类型。如果您使用的是 IComparer 接口,也许解决此任务的一种可能方法是。通过这种方式,您可以实现自定义排序行为。

我有类似的示例,其中我使用了 IComparer 接口。在那里我应该按绝对值对整数进行排序。您可以将此示例用作起点。有关 IComparer 接口的更多详细信息,请访问: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;
        }
    }

}
于 2013-07-31T11:04:55.107 回答