This is a migrated thread and some comments may be shown as answers.

RadGridView Sort

7 Answers 358 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Sonia
Top achievements
Rank 1
Sonia asked on 20 Jun 2014, 01:07 AM
Hi,

I am comparing Telerik RadGridView with Standard dataGrid. My requirement is to create grid with custom columns. Columns cellTemplate is a View itself and column sortmemberPath is set to property to display. This property is an object which could be anything (string/int/double/list etc. ) . With DataGrid sort was working but with Telerik RadGridView it's not working (the binding etc. is still the same.). Following is an example of one of the coulmn which is being templated as per the item.

Telerik.Windows.Controls.GridViewDataColumn columnTemplate = new Telerik.Windows.Controls.GridViewDataColumn();
columnTemplate.IsSortable = true;
columnTemplate.IsCustomSortingEnabled = true;
columnTemplate.MinWidth = (item as INode).Builder.Header.Width;

columnTemplate.Header = item;

columnTemplate.Width = (item as INode).Builder.Header.Width + 10;
if (!columnGroup.Equals(string.Empty))
{
columnTemplate.ColumnGroupName = columnGroup;
}

if (item is IntegerPropertyNodeViewModel)
{
var headerName = (item as IntegerPropertyNodeViewModel).PropertyName.Substring((item as IntegerPropertyNodeViewModel).PropertyName.LastIndexOf(' ') + 1);
columnTemplate.Header = headerName;
columnTemplate.UniqueName = headerName;

var text = new FrameworkElementFactory(typeof(IntegerPropertyNodeView));

if (0 != path.Length)
{
System.Windows.Data.Binding bind = new System.Windows.Data.Binding(path);
text.SetBinding(IntegerPropertyNodeView.DataContextProperty, bind);

columnTemplate.SortMemberPath = path + ".Target";
}
SdmGridControlViewColumnGenerator.SetName(columnTemplate, "IntegerProperty");
columnTemplate.CellTemplate = new DataTemplate();
columnTemplate.CellTemplate.VisualTree = text;
columnTemplate.CellTemplate.Seal();
dataGrid.Columns.Add(columnTemplate);
}

Could you please help me in sorting issue with Telerik RadGridView.

Thanks and Regards,
Sonia

7 Answers, 1 is accepted

Sort by
0
Dimitrina
Telerik team
answered on 20 Jun 2014, 02:24 PM
Hello Sonia,

You set the SortMemberPath to:
columnTemplate.SortMemberPath = path + ".Target";

This means the sorting will be done based on this property. You say this property is an object which could be anything (string/int/double/list etc.). It seems the column is not able to resolve the actual type of the bound data and that is why it is not able to sort the data. You will have to specify the correct GridViewDataColumn.DataType. 

Regards,
Didie
Telerik
 
Check out Telerik Analytics, the service which allows developers to discover app usage patterns, analyze user data, log exceptions, solve problems and profile application performance at run time. Watch the videos and start improving your app based on facts, not hunches.
 
0
Sonia
Top achievements
Rank 1
answered on 22 Jun 2014, 09:34 PM
Hi Didie,

Thanks for your quick reply.
I set the dataType of the column as per your suggestion. I still can't get it sorted. Grid View changes the sort state on the column header but actual data is not being sorted. Also every time I click header I get four first chance excelption (application doesn't crash as exception is handled in Telerik).
A first chance exception of type 'System.ArgumentException' occurred in Telerik.Windows.Data.dll

If I replace the GridViewDataColumn with GridViewColumn (As one of the topic suggests to use GridViewColumn for composite properties), then there is no exception but then I can't set the dataType.
I wonder why same code works with MS DataGrid. I am opting for Telerik GridView as I require multi level header.
Could you please suggest me something in resolving this sort issue.

Really appreciate your help.
Thanks and Regards,
Sonia
0
Sonia
Top achievements
Rank 1
answered on 22 Jun 2014, 11:54 PM
Hi Didie,

I added the DataMemberBinding to the Column and it's no longer throwing an exception. But still not sorting the columns. Following is the snapshot of one of the columnTemplate.
if (item is IntegerPropertyNodeViewModel)
{
var headerName = (item as IntegerPropertyNodeViewModel).PropertyName.Substring((item as IntegerPropertyNodeViewModel).PropertyName.LastIndexOf(' ') + 1);
columnTemplate.Header = headerName;
columnTemplate.DataType = typeof(int);

var text = new FrameworkElementFactory(typeof(IntegerPropertyNodeView));

if (0 != path.Length)
{
System.Windows.Data.Binding bind = new System.Windows.Data.Binding(path);
columnTemplate.DataMemberBinding = new System.Windows.Data.Binding(path + ".Target");
text.SetBinding(IntegerPropertyNodeView.DataContextProperty, bind);
columnTemplate.SortMemberPath = path + ".Target";
}
SdmGridControlViewColumnGenerator.SetName(columnTemplate, "IntegerProperty");
columnTemplate.CellTemplate = new DataTemplate();

columnTemplate.CellTemplate.VisualTree = text;
columnTemplate.CellTemplate.Seal();
dataGrid.Columns.Add(columnTemplate);
}

The ItemSource is an observablecollection of a class with children as viewmodels (of different data types). Basically I need to display children target(value) in the cell and I am customizing the column as per the children viewmodel type.

I can't just get my head around in fixing this trival issue. If MS DataGrid can sort it, why can't Telerik GridView.

Thanks and Regards,
Sonia
0
Dimitrina
Telerik team
answered on 23 Jun 2014, 09:31 AM
Hi Sonia,

The sorting is a data operations. This is how RadGridView is designed to work and all filtering, sorting and grouping is actually done by building and executing a LINQ query over the source collection. 

So, if you can build such a LINQ query appending OrderBy clause based on the SortMemberPath set for a column, then RadGridView will also be able to sort the respective column. Otherwise, this will not be possible.
If you can build a valid LINQ query and RadGridView still does not sort, then may I ask you to isolate the case in a demo project which I can check locally? 

You should set SortMemberPath to a property that you can sort on, or you can alternatively consider implementing IComparable for the bound data object as demonstrated on the "Custom Sorting with IComparable" WPF Demo . 

Regards,
Didie
Telerik
 
Check out Telerik Analytics, the service which allows developers to discover app usage patterns, analyze user data, log exceptions, solve problems and profile application performance at run time. Watch the videos and start improving your app based on facts, not hunches.
 
0
Sonia
Top achievements
Rank 1
answered on 24 Jun 2014, 01:49 AM
Hi Didie,
Thanks for your reply.
I tried orderby and sort on the grid. I can see the list being sorted, but the grid is not
reflecting the changes.

I have written a small application for your reference. It's very basic, but my
main application is quite complex. But I can replicate the sort issue in this
as well. I don't want to use OrderBy as this will require the Grid ITem source
reset. Sort does what I need, the only issue is telerik grid refresh. I am
attaching the sample app.

I will really appreciate if you can help me in this issue. How do I attach the app zip file. It supports only images. I have sent an email to clientService with the attachment, if that will help.

Thanks and Regards,
Sonia
0
Sonia
Top achievements
Rank 1
answered on 24 Jun 2014, 03:33 AM
Hi Didie,

I guess I need to set the Grid ItemSource as the new sorted items. But if I do that GridColumn won't set the sorting state. It is always None no matter how many times I click on the column. I actually tested the sorting by ordering the items in descending order when sorting state is none. So first time it displays in correct order but as the sorting state is always none, it won;t execute the else statements. I am copying the code.

public class TabularDataGridBehavior : Behavior<RadGridView>
{

protected override void OnAttached()
{
base.OnAttached();

AssociatedObject.Sorting += new EventHandler<GridViewSortingEventArgs>(AssociatedObject_Sorting);
// AssociatedObject.Sorted += new EventHandler<GridViewSortedEventArgs>(AssociatedObject_Sorted);

}

void AssociatedObject_Sorted(object sender, GridViewSortedEventArgs e)
{
int test = 0;

}

void AssociatedObject_Sorting(object sender, GridViewSortingEventArgs e)
{
//Gets the value of the ItemsSource property as IEnumerable.
RadGridView grid = (RadGridView)sender;
IEnumerable<INode> items = grid.ItemsSource as IEnumerable<INode>;

//Checks if the value of the collection is null.
if (items == null)
{
e.Cancel = true;
return;
}

//If the sorting state is none, sort the items ascending.
if (e.OldSortingState == SortingState.None)
{
e.NewSortingState = SortingState.Ascending;
//items.ToList().Sort(new GenericComparer<INode>(e.Column.DisplayIndex, SortOrder.Ascending));
grid.ItemsSource = items.OrderByDescending(x => (x as DataItemNodeViewModel).Children.ElementAt(e.Column.DisplayIndex) as BasePropertyNode, new nodeItemComparer()).ToList();
}
else if (e.OldSortingState == SortingState.Ascending)
{
e.NewSortingState = SortingState.Descending;
// items.ToList().Sort(new GenericComparer<INode>(e.Column.DisplayIndex, SortOrder.Descending));

grid.ItemsSource = items.OrderByDescending(x => (x as DataItemNodeViewModel).Children.ElementAt(e.Column.DisplayIndex) as BasePropertyNode, new nodeItemComparer()).ToList();
}
//If the sorting state is none, sort the items descending.
else
{
e.NewSortingState = SortingState.None;
}

e.Cancel = true;
e.Column.SortingState = e.NewSortingState;
}

protected override void OnDetaching()
{
AssociatedObject.Sorting -= new EventHandler<GridViewSortingEventArgs>(AssociatedObject_Sorting);
AssociatedObject.Sorted -= new EventHandler<GridViewSortedEventArgs>(AssociatedObject_Sorted);
}

}

public class nodeItemComparer : IComparer<BasePropertyNode>
{
#region IComparer<BasePropertyNode> Members

public nodeItemComparer()
{
}

int IComparer<BasePropertyNode>.Compare(BasePropertyNode x, BasePropertyNode y)
{
if (x is StringPropertyNodeViewModel)
{
if (null != x.Target && null != y.Target)
{
return string.Compare(x.Target.ToString(), y.Target.ToString());
}
}
return 0;
}

#endregion
}

Can you suggest the way out of this issue?

Thanks and Regards,
Sonia
0
Dimitrina
Telerik team
answered on 24 Jun 2014, 02:36 PM
Hello Sonia,

In your previous reply you say that you can replicate the issue in a sample project and you have attached it. It seems you were not able to attach it as this is not allowed in forums.

May I ask you to open a new support thread and send me the sample there? That way I will be able to check it locally and advise further.

Regards,
Didie
Telerik
 
Check out Telerik Analytics, the service which allows developers to discover app usage patterns, analyze user data, log exceptions, solve problems and profile application performance at run time. Watch the videos and start improving your app based on facts, not hunches.
 
Tags
GridView
Asked by
Sonia
Top achievements
Rank 1
Answers by
Dimitrina
Telerik team
Sonia
Top achievements
Rank 1
Share this question
or