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
close icon

sort on different column than user selected

Hello,
I'm using a griddataboundgrid, version 3.2.1.0, in a Windows form. I have a need to sort the grid's contents on a different column than the user selects. For instance, say the grid has five columns. The user clicks on the column header 2, but I want the grid to really sort on column header 4. Any way I can accomplish this?

Thanks.

5 Replies

AD Administrator Syncfusion Team December 15, 2006 05:13 AM UTC

Hi Jimmyd,

You would turn off the default sorting with the SortBehavior property. Then handle CellDoubleClick and do the sorting using Binder.Sort() method. Here is a code snippet to show this.

//turn OFF the Default sortbehavior....
this.gridDataBoundGrid1.SortBehavior = GridSortBehavior.None;
this.gridDataBoundGrid1.CellDoubleClick +=new GridCellClickEventHandler(gridDataBoundGrid1_CellDoubleClick);

private void gridDataBoundGrid1_CellDoubleClick(object sender, GridCellClickEventArgs e)
{
GridDataBoundGrid grid = sender as GridDataBoundGrid;
int fieldIndex = grid.Binder.ColIndexToField(e.ColIndex);
if( e.ColIndex == 2)
{
fieldIndex = grid.Binder.ColIndexToField(4);
}
if( fieldIndex != -1)
{
grid.BeginUpdate();
grid.Binder.Sort(fieldIndex);
grid.EndUpdate(true);
grid.Refresh();
}
}

Please refer to the attached sample for implementation.
ChangeSortColumnbehavior.zip

Best Regards,
Haneef


AD Administrator Syncfusion Team December 15, 2006 04:35 PM UTC

Hi,

Okay, I see the method you're describing. Thanks very much for the response. I'll try this out.



AD Administrator Syncfusion Team December 15, 2006 08:01 PM UTC

Hello,

This worked great - thanks for the help. I forgot to mention that I'm using VB.Net, but your code sample was easy to translate. Worked great.

Being greedy here, but if anyone knows of a way to do this, it would be helpful. Would you have any way to manage the Up and Down arrow graphic in the column that is double-clicked? (since it ends up in the target column that is actually being sorted)

Looks like I can capture the direction of the sort by something like this, but wasn't sure if I was going in the right direction.

Dim dt As New DataTable
Dim strSort as String
dt = myGridDataBoundGrid.DataSource
strSort = dt.DefaultView.Sort

Thanks again.


AD Administrator Syncfusion Team December 18, 2006 04:59 AM UTC

Hi Jimmyd,

Use the SortDirection property of the IBindingList to find the sort direction of the grid. Here is a code snippet to show this.

CurrencyManager lm = this.BindingContext[this.gridDataBoundGrid1.DataSource ] as CurrencyManager;
ListSortDirection listSortDirection = GetSortDirection(lm.List);
Console.WriteLine("Current Sort Direction :" + listSortDirection );

///Here is method to get the Sort Direction.......
public ListSortDirection GetSortDirection(IList list)
{
if (list is IBindingList && ((IBindingList)list).SupportsSorting)
return ((IBindingList)list).SortDirection;
return ListSortDirection.Ascending;
}

Best Regards,
Haneef


AD Administrator Syncfusion Team December 18, 2006 03:18 PM UTC

Hi Haneef,

That works great. I greatly appreciate the response. You guys rule.

jimmyd

Loader.
Live Chat Icon For mobile
Up arrow icon