Programmatically select the first row and column after adding a sortcolumn or changing the filter
Hello everyone,
we're looking for a way to implement an automatic selection of the first row and scrolling it into view after we've filtered or sorted our SfDataGrid.
So far it seems like the OnSortColumnsChanged Event does trigger on clicking a columnheader, but the column itself is not sorted while we debug the method.
private void DataGridKdAnalyse_OnSortColumnsChanged(object sender, GridSortColumnsChangedEventArgs e)
{
if (DataGridKdAnalyse.View != null && DataGridKdAnalyse.View.Records.Any())
{
if (DataGridKdAnalyse.View.Records.Count > 10)
{
for (int i = 0; i < 10; i++)
{
Console.WriteLine(((KdAnalyseHead)DataGridKdAnalyse.View.Records[i].Data).CustId);
}
}
this.DataGridKdAnalyse.SelectedItem = null;
this.DataGridKdAnalyse.ScrollInView(new RowColumnIndex(this.DataGridKdAnalyse.GetFirstRowIndex(),this.DataGridKdAnalyse.ResolveToStartColumnIndex()));
}
}
{
if (DataGridKdAnalyse.View != null && DataGridKdAnalyse.View.Records.Any())
{
if (DataGridKdAnalyse.View.Records.Count > 10)
{
for (int i = 0; i < 10; i++)
{
Console.WriteLine(((KdAnalyseHead)DataGridKdAnalyse.View.Records[i].Data).CustId);
}
}
this.DataGridKdAnalyse.SelectedItem = null;
this.DataGridKdAnalyse.ScrollInView(new RowColumnIndex(this.DataGridKdAnalyse.GetFirstRowIndex(),this.DataGridKdAnalyse.ResolveToStartColumnIndex()));
}
}
We would like to set the SelectedItem to the first record after sorting the column.
Kind regards,
Maxim
SIGN IN To post a reply.
5 Replies
1 reply marked as answer
MA
Mohanram Anbukkarasu
Syncfusion Team
June 26, 2020 08:54 AM UTC
Hi Maxim,
Thanks for contacting Syncfusion support.
This can be solved by using dispatcher to perform your selection related actions within the SortColumnsChanged event. So that once the background sorting process is complete the code block will be executed. Please refer the following code example.
Code example :
|
private void OnDataGrid_SortColumnsChanged(object sender, Syncfusion.UI.Xaml.Grid.GridSortColumnsChangedEventArgs e)
{
this.dataGrid.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.ContextIdle, new Action(() =>
{
if (dataGrid.View != null && dataGrid.View.Records.Any())
{
this.dataGrid.SelectedItem = this.dataGrid.View.Records[0];
this.dataGrid.ScrollInView(new RowColumnIndex(this.dataGrid.GetFirstRowIndex(), this.dataGrid.ResolveToStartColumnIndex()));
}
}));
} |
We have prepared a sample using the above given code example and it is available in the following link for your reference.
Sample link : https://www.syncfusion.com/downloads/support/directtrac/general/ze/SfDataGrid_Demo363580145
Please let us know if you require further assistance from us.
Regards,
Mohanram A.
Marked as answer
MV
Maxim Voss
June 26, 2020 10:09 AM UTC
Thank you for your fast reply,
This worked perfectly fine, but somehow the visible Border of our SfDataGrid changes to red. Maybe you could give me a hint what to look for.
Is there some kind of validation?
Regards Maxim
Attachment: SfDataGridBorderChangedAfterSorting_6dfca448.zip
SS
Susmitha Sundar
Syncfusion Team
June 29, 2020 02:33 PM UTC
Hi Maxim,
Thank you for the update.
Based on the provided information, we have checked the mentioned issue “visible Border of our SfDataGrid changes to red” and we unable to replicate the issue from our end, it is working fine as expected.
Please modify the sample based on your scenario and revert us back with following details,
1. Video reference (if possible)
2. Syncfusion updated version
3. SfDataGrid settings
It will be helpful for us to check on it and provide you the solution at the earliest.
Regards,
Susmitha S
Susmitha S
MV
Maxim Voss
June 30, 2020 11:22 AM UTC
So we've figured it out..
we've bound the SelectedItem Property of our SfDataGrid to the Model Type.
Based on your solution we've set the SelectedItem to a RecordEntry Object instead of our Model causing an Exception.
System.Windows.Data Error: 23 : Cannot convert 'Syncfusion.Data.RecordEntry' from type 'RecordEntry' to type 'SfDataGridError.KdAnalyseKopf' for 'de-DE' culture with default conversions; consider using Converter property of Binding. NotSupportedException:'System.NotSupportedException: TypeConverter kann nicht von Syncfusion.Data.RecordEntry konvertieren.So instead we just use the Data Property of the first RecordEntry to avoid using a Converter.
bei System.ComponentModel.TypeConverter.GetConvertFromException(Object value)
bei System.ComponentModel.TypeConverter.ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, Object value)
bei MS.Internal.Data.DefaultValueConverter.ConvertHelper(Object o, Type destinationType, DependencyObject targetElement, CultureInfo culture, Boolean isForward)'
System.Windows.Data Error: 7 : ConvertBack cannot convert value 'Syncfusion.Data.RecordEntry' (type 'RecordEntry'). BindingExpression:Path=SelectedCustomer; DataItem='KdAnalyseVM' (HashCode=38496415); target element is 'SfDataGrid' (Name='DataGridKdAnalyse'); target property is 'SelectedItem' (type 'Object') NotSupportedException:'System.NotSupportedException: TypeConverter kann nicht von Syncfusion.Data.RecordEntry konvertieren.
bei MS.Internal.Data.DefaultValueConverter.ConvertHelper(Object o, Type destinationType, DependencyObject targetElement, CultureInfo culture, Boolean isForward)
bei MS.Internal.Data.ObjectTargetConverter.ConvertBack(Object o, Type type, Object parameter, CultureInfo culture)
bei System.Windows.Data.BindingExpression.ConvertBackHelper(IValueConverter converter, Object value, Type sourceType, Object parameter, CultureInfo culture)'
this.DataGridKdAnalyse.SelectedItem = this.DataGridKdAnalyse.View.Records[0].Data;
Thank you for your help and time
Maxim
SS
Susmitha Sundar
Syncfusion Team
July 1, 2020 02:08 PM UTC
Hi Maxim,
Thanks for the update.
We are glad to know that the reported problem has been resolved at your end. Please let us know if you have any further queries on this.
Regards,
Susmitha S
SIGN IN To post a reply.
- 5 Replies
- 3 Participants
- Marked answer
-
MV Maxim Voss
- Jun 26, 2020 07:40 AM UTC
- Jul 1, 2020 02:08 PM UTC