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

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)