We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

GridControl - Sorting

Hi -
I was looking improve the performance of sorting large sets of data and followed the instructions in the following forum post (using the referenced sample).
http://www.syncfusion.com/Support/Forums/message.aspx?MessageID=34771

It definitely speeds things up, but it is sorting my float column like strings even though i am setting the CellValueType to Double. When the GridRowSortItem does the compare, it''s of 2 objects and thus the sorting is incorrect.

Any suggestions or other samples on how i can propertly sort different data types?

Thanks,
Julie


3 Replies

AD Administrator Syncfusion Team September 6, 2006 03:52 AM UTC

Hi Julie,

The GridControlSort browser sample shows you how to add header-click sorting to your grid. The header cell draws a sort icon to indicate that the column has been sorted in a particular direction.

Here is a path.
[Instal drive]: \Program Files\Syncfusion\Essential Studio\4.3.0.0\windows\Grid.Windows\Samples\Quick Start\GridControlSort

Click the syncfusion Browser Sample=>Essetail Studio samples => GridSamples => QuickStart => GridControlSort

Let me know if this helps.
Thanks,
Haneef


JL Julie Levy September 6, 2006 05:09 PM UTC

Hi Haneef -
Thanks for your quick response.
The sample you are suggesting shows how to do custom sorting for a formula column and uses SortByColumn. I found SortByColumn to be very slow which is why i went with the other example which uses an Array sort. It doesn''t, however, recognize the cellvaluetypes when it does the compare. I found a way to do it by getting the cellvaluetype out using model indexing when building the array, storing it in the GridRowSortItem, and then checking it during the compare. (see code below) This has to be slowing things down so i was just wondering if there was a better way.

public struct GridRowSortItem : IComparable
{
public GridCellCollection row;
public object sortValue;
public Type valueType;
#region IComparable Members

public int CompareTo( object obj )
{
int iResult = 0;
object val1 = sortValue;
object val2 = obj != null ? ((GridRowSortItem)obj).sortValue : null;
double double1 = 0;
int int1 = 0;
int int2 = 0;
double double2 = 0;

if (ObjectIsNullOrEmpty( val1 ) && ObjectIsNullOrEmpty(val2))
return 0;
else if (ObjectIsNullOrEmpty(val1))
return -1;
else if (ObjectIsNullOrEmpty( val2 ))
return 1;
else
{
try
{
if (val1 is IComparable && val2 is IComparable)
{
if (valueType == typeof( Double ))
{
double1 = Convert.ToDouble(val1);
double2 = Convert.ToDouble(val2);
iResult = double1.CompareTo( double2 );
}
else if (valueType == typeof( Int32 ))
{
int1 = Convert.ToInt32( val1 );
int2 = Convert.ToInt32( val2 );
iResult = int1.CompareTo( int2 );
}

else
{
iResult = ((IComparable)val1).CompareTo( val2 );
}

//int r = ((IComparable)val1).CompareTo( val2 );
//Trace.WriteLine(val1.ToString() + " / " + val2.ToString() + " : " + r.ToString());
return iResult;
}
}
catch (Exception ex)
{
}

return val1.ToString().CompareTo( val2.ToString() );
}

}


protected virtual void SortData( int columnIndex )
{
long start = DateTime.Now.Ticks;

GridRowCollection gridRows = this.Data.Rows;

int headerCount = this.Rows.HeaderCount + 1 + 1; // 1 extra for column styles info row
int colnum = columnIndex + 1; // 1 extra for row styles info column
int rowCount = gridRows.Count - headerCount;
GridRowSortItem[] sortArray = new GridRowSortItem[rowCount];
for (int n = 0; n < rowCount; n++)
{
sortArray[n] = new GridRowSortItem();
GridCellCollection row = gridRows[n + headerCount];
if (row != null)
{
sortArray[n].row = row;
GridStyleInfoStore style1 = row[colnum];
GridStyleInfo style2 = this.Model[n + headerCount-1, columnIndex];
if (style1 != null)
{
object val1 = style1.GetValue( GridStyleInfoStore.CellValueProperty );
sortArray[n].sortValue = val1;
sortArray[n].valueType = style2.CellValueType;
}
}
}

Array.Sort( sortArray );

for (int n = 0; n < rowCount; n++)
{
gridRows[n + headerCount] = sortArray[n].row;
}

long end = DateTime.Now.Ticks;
this.EndUpdate();
this.Refresh();


//string str = "Column " + columnIndex + " took " + (end - start).ToString( "N0" ) + " ticks";
//System.Diagnostics.Debug.WriteLine( str );

Cursor.Current = Cursors.Default;

}




MI Miguel September 28, 2006 09:36 PM UTC

Hello my friends,

I would like to know instead how can I keep the user from sorting the columns.

Sometimes the users need to have the data in the original sequence it is being displayed but some of them sort the data unintentionally and the printouts are messed out.

So, my question is, how to disable the sorting ability of the control.

Any help is much appreciated.

Loader.
Live Chat Icon For mobile
Up arrow icon