HI guys,
I have an issue in Grid view, In columns filter the distinct values are showing but the sorting order was irrelevant. I want if I sort the values in the grid as descending then in columns filter the distinct values also want to shown in descending order but it always shown as default Ascending order. please help me am stuck in this issue.
15 Answers, 1 is accepted
In order to achieve the desired behavior you can handle RadGridView's DistinctValuesLoading event, like so:
private
void
ClubsGrid_DistinctValuesLoading(
object
sender, Telerik.Windows.Controls.GridView.GridViewDistinctValuesLoadingEventArgs e)
{
var gridView = sender
as
RadGridView;
if
(e.Column.SortingState == SortingState.Descending)
{
e.ItemsSource = gridView.GetDistinctValues(e.Column,
true
).OfType<
string
>().OrderByDescending(x => x);
}
}
Please let me know if this works for you.
Regards,
Dilyan Traykov
Telerik by Progress
HI
Thanks for the reply but it was not the fix to me, by default the grid sort was none at the time itself the filter distinct values are in Asc. I need if I change the sort order in grid then the filter distinct values also sort based on the grid sort. It always remain in default asc order.
Thanks for the reply but it was not the fix to me, The grid default sort order was none at the time itself the filter distinct values are sort by asc as default. I need if the grid was sort by asc or desc or none then the filter distinct values also sort based on the grid sorting order.
Thanks for the reply but it was not the fix to me, The grid default sort order was none at the time itself the filter distinct values are sort by asc as default. I need if the grid was sort by asc or desc or none then the filter distinct values also sort based on the grid sorting order.
HI
Thanks it was fixed the issue for both the Asc and Desc order. But the grid values was sort by the primary key, so the filter distinct value sorting is none means then it sort by the primary key. right now if the sorting was none means by default it takes as asc order.
Private Sub grdMessages_DistinctValuesLoading(ByVal sender As Object, e As Telerik.Windows.Controls.GridView.GridViewDistinctValuesLoadingEventArgs) Handles grdMessages.DistinctValuesLoading
Dim gridView = TryCast(sender, RadGridView)
If e.Column.SortingState = SortingState.Descending Then
e.ItemsSource = gridView.GetDistinctValues(e.Column, True).OfType(Of String)().OrderByDescending(Function(x) x)
ElseIf e.Column.SortingState = SortingState.Ascending Then
e.ItemsSource = gridView.GetDistinctValues(e.Column, True).OfType(Of String)().OrderBy(Function(x) x)
ElseIf e.Column.SortingState = SortingState.None Then
e.ItemsSource = ? 'confused in this part
End If
End Sub
You can place the following snippet inside the SortingState.None if statement in order to achieve the desired result:
Dim
clubs = (TryCast(gridView.ItemsSource, IEnumerable(Of Club))).ToList()
Dim
propertyName = (TryCast(e.Column, GridViewBoundColumnBase)).DataMemberBinding.Path.Path
e.ItemsSource = clubs.
Select
(
Function
(c)
GetType
(Club).GetProperty(propertyName).GetValue(c)).Distinct()
Note that Club should be replaced with the type of your bound business objects.
Please let me know if this works for you.
Regards,
Dilyan Traykov
Telerik by Progress
Hi
Thanks for the reply it was really helpful to me but in that scenario am not using the BO so I made little change and its working.
ElseIf e.Column.SortingState = SortingState.None Then
Dim selectedDataRow = From dr In DirectCast(gridView.ItemsSource, C1.Silverlight.Data.DataView).Table.Rows.AsEnumerable()
Dim propertyName = (TryCast(e.Column, GridViewBoundColumnBase)).DataMemberBinding.Path.Path
e.ItemsSource = From result In selectedDataRow
Select result.Item(propertyName) Distinct
End If
I'm glad to hear that you found the provided information helpful. Should any further questions or concerns arise, do not hesitate to contact me
Regards,
Dilyan Traykov
Telerik by Progress
Hi
I have found another issues on my code that I used if sorting state was none in a column its working correctly for that particular column by this code.
ElseIf e.Column.SortingState = SortingState.None Then
Dim selectedDataRow = From dr In DirectCast(gridView.ItemsSource, C1.Silverlight.Data.DataView).Table.Rows.AsEnumerable()
Dim propertyName = (TryCast(e.Column, GridViewBoundColumnBase)).DataMemberBinding.Path.Path
e.ItemsSource = From result In selectedDataRow
Select result.Item(propertyName) Distinct
End If
The real issue is I sort a particular column in grid desc then the distinct values in the column filter also sort out in desc but the other column in the grid was not sorted.
I need if any one column was sort means then the remaining columns filter distinct values in the grid also want to sort based on the grid order. Exact solution I want is if the column sort state was none means then the filter distinct values will populate based on the grid order.
In the attachement I sort out the subject column in desc the distinct values are also sort in desc for the column its correct but if I open the filer in "From" column it was not sort based on the grid order. It took the column sort state was none and it populate.
If I understood you correctly, what you need to modify in the previous snippet is to use RadGridView's Items collection which is of type DataItemCollection and represents a collection of the items after filtering, sorting and grouping operations have been applied upon the RadGridView rather than the ItemsSource collection.
Could you please give this a try and let me know if it works for you?
Regards,
Dilyan Traykov
Telerik by Progress
HI
I tried based on your instruction but it was not working to me. The ItemsSource collection value will took the default query value itself. I need if the sort state was none in column then the filter Distinct values in the isfilter will also populate the values order in the Gridview. for each and every column it has to perform based on the grid column order the same order values also populate in the filter Distinct values in the isfilter.
I'm afraid I was unable to understand your exact requirements from your last reply, but I am attaching a sample project to better demonstrate what I had in mind.
Could you please have a look at it and let me know if this is the behavior you're looking for? Even though the project is written in C#, you can use the Telerik Code Converter to translate this into VB.
If the desired result differs from the one in the sample project, please clarify your exact requirements and I will gladly assist you further.
Regards,
Dilyan Traykov
Telerik by Progress
Hi
Thanks for the quick reply but I got version incompatible issue occurring for your sample project am trying out with other version.what my requirement is,
Currently if I sort the particular Grid Column in desc means then that particular column filter distinct values only sort out in desc remaining other column sort state was none so it takes the query output.
If I sort any 1 column in the Grid at any sort order then the remaining columns filter distinct values also want to sort out. For Example, I sort 1 Column in desc order then the remaining columns also sort in desc in the Grid it's correct like that I want in the filter distinct values also.
Whatever the sort order in the Grid values the same order wants to be in the filter distinct values for all columns.
If I understood you correctly, I believe the last project I sent you successfully implements the desired behavior. I'm attaching a new one, targeting the Silverlight framework.
Could you please try running it and let me know if it works? If you're unable to run it - here is the snippet of interest:
Private
Sub
ClubsGrid_DistinctValuesLoading(
ByVal
sender
As
Object
,
ByVal
e
As
Telerik.Windows.Controls.GridView.GridViewDistinctValuesLoadingEventArgs)
Dim
gridView = TryCast(sender, RadGridView)
If
e.Column.SortingState = SortingState.Descending
Then
e.ItemsSource = gridView.GetDistinctValues(e.Column,
True
).OfType(Of
Object
)().OrderByDescending(
Function
(x) x)
ElseIf
e.Column.SortingState = SortingState.Ascending
Then
e.ItemsSource = gridView.GetDistinctValues(e.Column,
True
).OfType(Of
Object
)().OrderBy(
Function
(x) x)
Else
Dim
clubs =
New
List(Of Club)()
For
Each
item
In
gridView.Items
clubs.Add(TryCast(item, Club))
Next
item
Dim
propertyName = (TryCast(e.Column, GridViewBoundColumnBase)).DataMemberBinding.Path.Path
e.ItemsSource = clubs.
Select
(
Function
(c)
GetType
(Club).GetProperty(propertyName).GetValue(c,
Nothing
)).Distinct()
End
If
End
Sub
I will be awaiting your reply.
Regards,
Dilyan Traykov
Telerik by Progress
Hi
Sorry for the delay but really so thanks to you and finally I got the solution from your sample project its working for me as per the requirement. Thanks a lot.