所提到的行爲預期,因爲你的[時間]列有字符串的數據類型。也許一種可能的方法來解決這個任務,可能是如果你正在使用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;
}
}
}