0

我需要SortOrder在GridEx中设置属性,而这个属性只有get.

我能做些什么?

编码:

private void M_Grid_ColumnHeaderClick(object sender, Janus.Windows.GridEX.ColumnActionEventArgs e)
{
    if (e.Column.DataMember == "Filed1")
    {
        var list = m_Grid.DataSource;

        if (e.Column.SortOrder == Janus.Windows.GridEX.SortOrder.Descending)
        {
            list = list.OrderBy(p => p.ParticipationDate).ToList();
            e.Column.SortOrder = Janus.Windows.GridEX.SortIndicator.Ascending;// it's not good
        }
        else
        {
            list = list.OrderByDescending(p => p.ParticipationDate).ToList();
            e.Column.SortOrder = Janus.Windows.GridEX.SortIndicator.Descending;// it's not good
        }
        m_Grid.DataSource = list;
    }
}
4

2 回答 2

0

经过大量实验并得到正确答案。

如果您想按任何其他值而不是默认值对特定字段进行排序,请使用属性:SortComparer并实现类的Compare()功能IComparer

例如,我所做的是:

在组件的负载上写:

m_Grid.RootTable.Columns["Field1"].SortComparer = new Field1Sort();

要实现的类:

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

    public int Compare(object x, object y)
    {
        var xString = (string)x;
        var yString = (string)y;

        var date1 = xString.Substring(0, 10).ToDateTime();
        var date2 = yString.Substring(0, 10).ToDateTime();

        if (date1 < date2)
            return -1;
        if (date1 > date2)
            return 1;
        else
            return 0;
    }
}
于 2019-10-06T10:23:39.963 回答
0

不推荐的方式:

private void M_Grid_ColumnHeaderClick(object sender, Janus.Windows.GridEX.ColumnActionEventArgs e)
{
    if (e.Column.DataMember == "Filed1")
    {
        var list = m_Grid.DataSource;

        if (e.Column.SortOrder == Janus.Windows.GridEX.SortOrder.Descending)
        {
            list = list.OrderBy(p => p.ParticipationDate).ToList();
          //  e.Column.SortOrder = Janus.Windows.GridEX.SortIndicator.Ascending;
        }
        else
        {
            list = list.OrderByDescending(p => p.ParticipationDate).ToList();
         //   e.Column.SortOrder = Janus.Windows.GridEX.SortIndicator.Descending;
        }
        m_Grid.DataSource = list;
        m_Grid.Refetch();
    }
}

例如,另一种使用排序键的方法:

private void M_Grid_ColumnHeaderClick(object sender, Janus.Windows.GridEX.ColumnActionEventArgs e)
{
    if (e.Column.DataMember == "Filed1")
    {
         //Removing any sort key that may be present in the table
        m_Grid.RootTable.SortKeys.Clear(); //or can be removed only specific 
       // sortkey

        // get the column to be sorted
        column = e.Column;

        if (e.Column.SortOrder == Janus.Windows.GridEX.SortOrder.Descending)
        {
             sortKey = new GridEXSortKey(column, SortOrder.Ascending);
        }
        else
        {
          sortKey = new GridEXSortKey(column, SortOrder.Descending);
        }

       m_Grid.RootTable.SortKeys.Add(sortKey);
    }
}
于 2019-10-06T07:38:18.847 回答