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

Managing column sorting indices

7 Answers 350 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Magnus
Top achievements
Rank 1
Magnus asked on 27 Jun 2017, 06:01 AM

Hi, I've got a grid where one of my columns shows an alphanumeric id and I want to sort the numbers correctly so I've implemented manual sorting. The bound data comes from a DataTable so I don't have a viewmodel for the rows. My sorting code, which I've hooked up to the Sorting-event, looks something like this:

private void OnGridViewSorting(object sender, GridViewSortingEventArgs e)
{
  if (e.Column.IsCustomSortingEnabled)
  {
    var dataView = (DataView)e.DataControl.ItemsSource;
    if (e.NewSortingState == SortingState.None)
    {
      // TODO: We should maybe reset sorting, not just return here.
      return;
    }
        
    if (dataView.Table.Rows.Count > 0)
    {
      e.Cancel = true;
      DataTable dtNew;
      if (e.OldSortingState == SortingState.None)
      {
        e.NewSortingState = SortingState.Ascending;
        dtNew = dataView.Table.AsEnumerable().OrderBy(x => (string)x[e.Column.Name], GenericStringLogicalComparer.Default).CopyToDataTable();
      }
      else
      {
        e.NewSortingState = SortingState.Descending;
        dtNew = dataView.Table.AsEnumerable().OrderByDescending(x => (string)x[e.Column.Name], GenericStringLogicalComparer.Default).CopyToDataTable();
      }
      var existingDataView = (DataView)this.ItemsSource;
      for (int n = 0; n < existingDataView.Table.Rows.Count; ++n)
      {
        // Just copy the item array instead of rebuilding table
        existingDataView.Table.Rows[n].ItemArray = dtNew.Rows[n].ItemArray;
      }
      existingDataView.Table.AcceptChanges();
    }
  }
}

 

But I have two issues:

  1. When sorting on several columns I want to manage the sorting indices showing in the column headers, but I don't know how. It seems like they correspond to what's in the SortDescriptors collection. But I can't really edit the descriptors without adding some other sorting, can I? Is there some way to manually update the indices? Or set the SortDescriptors without really add another sort.
  2. We can store the state of grids to user-defined views, and do this by using the PersistenceManager to get xml describing the state of the grid. But since the sorting is defined by the SortDescriptors collection our manually sorting is not exported to xml.

I guess that both problems is about how I should edit the SortDescriptors when using the Sorting event.

7 Answers, 1 is accepted

Sort by
0
Magnus
Top achievements
Rank 1
answered on 16 Aug 2017, 11:06 AM
Still having an issue with this, does anyone have a clue?
0
aegir
Top achievements
Rank 1
answered on 04 Sep 2019, 08:15 AM

Hi,

I need it too.

 

Thanks

0
Martin Ivanov
Telerik team
answered on 11 Sep 2019, 02:28 PM

Hello,

Indeed, the order of the sort indexes depends on the index of the SortDescriptor in the SortDescriptors collection of RadGridView. To change the indexes, change the order of the SortDescriptor objects in the SortDescriptors collection. There is no API for setting the indexes manually without re-ordering the collection.

The SortDescriptors are not fully saved by default with the RadPersistenceManager. To save them, you can use a custom property provider as shown in the GridViewSerialization SDK example.

Regards,
Martin Ivanov
Progress Telerik

Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
0
aegir
Top achievements
Rank 1
answered on 11 Sep 2019, 02:42 PM

Hi,

We can't use sort description because we don't want to have automatic sorting without user action. (User request).

We would like to be able to control it manually.

Best regards

0
Martin Ivanov
Telerik team
answered on 16 Sep 2019, 08:40 AM

Hello Stephan,

You don't need to use custom sorting (without user interaction), you can simply re-order the SortDescriptors collection of RadGridView when need to adjust the indexes. I've attached a small example showing this. Can you check it and let me know if I got this correctly?

Regards,
Martin Ivanov
Progress Telerik

Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
0
aegir
Top achievements
Rank 1
answered on 16 Sep 2019, 09:38 AM

Your Sample doesn't works. 

What we would like, when we sort or multi sort the grid, we would like to see the number on top of header and

if user edit a cell sorted, the sort is not applied automatically until the user decide to do it, or allow it.

0
Martin Ivanov
Telerik team
answered on 19 Sep 2019, 06:05 AM

Hello Stephan,

I've tested this on my side, and the sort order in the columns gets update when you change the value of a cell using the UI. However, the sort index of the column won't get updated because the sort direction is not changed in this case, but only an item is moved according to the current direction.

If want a behavior where the sort index of the column changes whenever a cell gets updated, you can disable the default sort indexes, and define a custom DataTemplate for the column headers in order to add custom UI for the headers. There you can include a TextBlock element and bind it to a property representing a custom sort order, which you can set manually. To apply the template, use the HeaderCellStyle property of the RadGridView columns.

<telerik:GridViewDataColumn.HeaderCellStyle>
	<Style TargetType="telerik:GridViewHeaderCell">
		<Setter Property="ContentTemplate">
			<Setter.Value>
				<DataTemplate>
					<StackPanel Orientation="Horizontal">
						<TextBlock Text="Id" />
						<TextBlock Text="{Binding MyIdColumnSortIndex}" />
					</StackPanel>
				</DataTemplate>
			</Setter.Value>
		</Setter>
	</Style>
</telerik:GridViewDataColumn.HeaderCellStyle>

Regards,
Martin Ivanov
Progress Telerik

Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
Tags
GridView
Asked by
Magnus
Top achievements
Rank 1
Answers by
Magnus
Top achievements
Rank 1
aegir
Top achievements
Rank 1
Martin Ivanov
Telerik team
Share this question
or