0

I need to set the property SortOrder in the GridEx, and this property is only get.

What can I do?

the code:

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;
    }
}
Ayal
  • 133
  • 1
  • 1
  • 11
  • Why do you need perform this manually if you are working with Janus will be done by default, you can look at design time of a grid control and can define the sort defaults for each column and other many options.. – Oleg Oct 06 '19 at 07:29
  • I know, but this is the grid now in the big project, and I can't change it on time... – Ayal Oct 06 '19 at 07:33

2 Answers2

0

Not recommended way:

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();
    }
}

Another approach working with sortkeys for example:

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);
    }
}
Oleg
  • 3,580
  • 1
  • 7
  • 12
0

After a lot of experimenting and being the right answer.

In case you want to sort a particular field by any other value and not by default Use the property: SortComparer and implement the Compare() function of the IComparer class

For example, what I did is:

On the load of the component write:

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

The class to implement:

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;
    }
}
Ayal
  • 133
  • 1
  • 1
  • 11