Home Forums WinForms controls Xceed Grid for WinForms Sorting columns in an exceed grid which has a viewer

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • User (Old forums)
    Member
    Post count: 23064
    #15793 |

    Hi,
    I have a grid column which is associated with a comboboxviewer. I find a problem with the viewer when I try to sort this column. On sorting the column is sorted based on the underlying value in the datasource rather than the value displayed by the viewer

    Let’s say I have a viewer. I have a datasource which says
    Id Value
    0 d
    1 c
    2 b
    3 a

    I associated this viewer with a grid column and sorted the column in ascending order. Now I expect the column to be sorted in this order a, b, c, d. But the sorting happens based on the Id and I get the result to be d, c, b, a.

    1. How can I solve the above problem?
    2. I looked at something called DataComparer but this was not helpful in this specific scenario. Am I missing something?

    Thanks,
    Praburaj

    Imported from legacy forums. Posted by Praburaj (had 3721 views)

    Xceed Support
    Member
    Post count: 5658

    The sorting will be done using the Value of the Cell.

    To change that behavior, you will indeed need to use a custom DataComparer.

    We have a <a href=”http://doc.xceedsoft.com/products/XceedGrid/Custom_Sorting.html”>Topic</a&gt; regarding the exact subject.

    Imported from legacy forums. Posted by CharlesB (had 273 views)

    User (Old forums)
    Member
    Post count: 23064

    Yes. I saw that already. Here is the problem if I implement a custom DataComparer and assign it to this column still the value considered for comparison is only 0, 1, 2 and 3 and not the alphabets a, b, c and d being displayed using the viewer. Do we have a way to say that pass the value displayed using the viewer rather than the underlying data 0, 1, 2 and 3.

    Thanks!
    Praburaj

    Imported from legacy forums. Posted by Praburaj (had 390 views)

    Xceed Support
    Member
    Post count: 5658

    To get this behavior, in your Custom DataComparer, you get the Viewer value and you use it to sort.

    Here is a short sample:

    To do this, I created a custom viewer (“MyViewer.cs”) and a custom DataComparer (“MyDataComparer.cs”).

    My custom viewer to which I added a GetTextToSortBy() method:

    <code>
    class MyViewer: TextViewer
    {
    public MyViewer()
    : base()
    {
    }

    public MyViewer( WinTextBox template )
    : base( template )
    {
    }

    protected override string GetTextCore( object value, CellTextFormatInfo formatInfo, GridElement gridElement )
    {
    if( ( int )value == 0 )
    value = “D”;
    else if( ( int )value == 1 )
    value = “B”;
    else if( ( int )value == 2 )
    value = “A”;
    else
    value = “C”;

    return base.GetTextCore( value, formatInfo, gridElement );
    }

    public string GetTextToSortBy( int cellValue )
    {
    string value;

    if( cellValue == 0 )
    value = “D”;
    else if( cellValue == 1 )
    value = “B”;
    else if( cellValue == 2 )
    value = “A”;
    else
    value = “C”;

    return value;
    }
    }
    </code>

    My custom DataComparer:
    <code>
    class MyDataComparer:IComparer
    {
    public MyDataComparer()
    {
    }

    int IComparer.Compare( object x, object y )
    {
    if( x is int && y is int )
    {
    //Using the GetTextToSortBy() method to get the viewer value
    MyViewer mv = new MyViewer();
    string XValue = mv.GetTextToSortBy( ( int )x );
    string YValue = mv.GetTextToSortBy( ( int )y );

    return XValue.CompareTo( YValue );
    }
    else
    return 0;
    }
    }
    </code>

    My main code (I just dropped a GridControl on my form):
    <code>
    private void Form1_Load( object sender, EventArgs e )
    {
    Column col1 = new Column( “Column1” );
    Column col2 = new Column( “Column2”, typeof(int) );
    gridControl1.Columns.Add( col1 );
    gridControl1.Columns.Add( col2 );

    for( int i = 0; i < 20; i++ )
    {
    Xceed.Grid.DataRow row = gridControl1.DataRows.AddNew();
    row.Cells[ “Column1” ].Value = “Item ” + i.ToString();
    row.Cells[ “Column2” ].Value = i%4;
    row.EndEdit();
    }

    gridControl1.Columns[ “Column2” ].CellViewerManager = new MyViewer();
    gridControl1.Columns[ “Column2” ].DataComparer = new MyDataComparer();
    }
    </code>

    Imported from legacy forums. Posted by CharlesB (had 3891 views)

    User (Old forums)
    Member
    Post count: 23064

    This isn’t a generic solution though – it requires that your comparer “knows” about the mapping of base values to text representations. Ideally there should be a way a comparer can be given both the DisplayMember and ValueMember values from the viewer.

    A workaround is to build a Comparer that takes as argument the DataTable that contains the mapping (the same table used by the viewer as its datasource) – a strongly typed table would allow Find operations to get the descriptions, but then you’d need one comparer per entity that must be supported.

    Imported from legacy forums. Posted by Jeremy (had 980 views)

Viewing 5 posts - 1 through 5 (of 5 total)
  • You must be logged in to reply to this topic.