Hi,
I am currenlty evaluating the FlyGrid control and trying to develop one of my dialogs using evaluation version 1.4.0.25923. Depending on whether I am successfull or not I could recommend our company to buy a real license.
I am experiencing problems with sorting similar to the problems described here: http://www.9rays.net/forums/viewtopic.php?t=853
I have following questions:
1. Am I understanding right, that the FlyGrid CAN NOT sort rows in bound mode by default (no additional coding from my side, only with Columns.Options.ChangeSortOrderOnClick=true set in the designer) if the DataSource is an array, which implements an IList interface?
Is the custom sorting required in this case? Should I use my own IComparer implementation?
Here is how I am populating grid with data:
private void Form1_Load(object sender, System.EventArgs e)
{
ArrayList list = new ArrayList();
Data d1 = new Data(1, \"test1\" );
Data d2 = new Data(2, \"test2\" );
Data d3 = new Data(3, \"test3\" );
Data d4 = new Data(4, \"test4\" );
Data d5 = new Data(5, \"test5\" );
list.Add( d1 );
list.Add( d2 );
list.Add( d3 );
list.Add( d4 );
list.Add( d5 );
flyGrid1.Rows.DataSource = list;
}
2. In order to apply sorting in bound mode I tried to subscribe to grid's ColumnSortOrderChanging event, make my own IComparer interface implementation and to call flyGrid.Rows.Items.Sort(new MyIComparer()) in the ColumnSortOrderChanging event handler:
private void flyGrid1_ColumnSortOrderChanging(object sender, NineRays.Windows.Forms.Grids.Column column, System.Windows.Forms.SortOrder value, ref bool sorted)
{
MyFlyGrid fg = sender as MyFlyGrid;
// I have tried every single from following commented lines:
// fg.Rows.Items.Sort(new MyIComparer(column,value));
// fg.Rows.Items.Sort(null);
// fg.Rows.Items.Sort();
fg.Rows.RefreshNodes();
sorted = true;
}
I have also tried to override GetComparer() method of the FlyGrid class:
public class MyFlyGrid : FlyGrid
{
public MyFlyGrid():base()
{
}
protected override IComparer GetComparer(Rows rows, Column[] sortColumns)
{
return new MyComparer(sortColumns);
}
}
Here is an IComparer implementation, which I have made following provided samples:
public class MyComparer : IComparer
{
private SortOrder order;
private Column col;
private Column[] cols;
public MyComparer(Column col, SortOrder order)
{
this.col = col;
this.order = order;
}
public MyComparer(Column[] col)
{
this.cols = col;
this.order = SortOrder.Descending;
}
#region IComparer Members
public int Compare(object x, object y)
{
Node xnode = x as Node;
Node ynode = y as Node;
int result = 0 ;
object xobj = xnode.GetCellValue(col);
object yobj = ynode.GetCellValue(col);
IComparable xvalue = xobj as IComparable;
IComparable yvalue = yobj as IComparable;
if (xvalue != null && order == SortOrder.Ascending)
{
result = xvalue.CompareTo(yvalue);
}
else if (yvalue != null && order == SortOrder.Descending)
{
result = yvalue.CompareTo(xvalue);
}
return result;
}
#endregion
}
I have also tried to make my data class implement IComparable interface.
But I did not succeed to sort my grid on column header click.
Actually, neither GetComparer() nor Compare() nor CompareTo() methods did not get called during execution in any of my attempts, although the ColumnSortOrderChanging event gets fired fine.
This is my sample data class:
public class Data : IComparable
{
private string _ID;
public string ID
{
get{ return _ID; }
set{ _ID = value; }
}
private int _Value;
public int Value
{
get{ return _Value; }
set{ _Value = value; }
}
public Data(string id, int val)
{
_ID = id;
_Value = val;
}
#region IComparable Members
public int CompareTo(object obj)
{
Data d = obj as Data;
if( d != null )
return this.Value.CompareTo(d.Value);
return 0;
}
#endregion
}
Could you please give me a hint, what am I doing wrong?
Thank you.