I am using the sfdatagrid to display some data.
I have implemented features to move selected row up, down, to top and to bottom of the datagrid with a button click event.
When the selected row is moved it should still be highlighted, this part works for moving a row up, down and to bottom but for some reason it does not work when moving the row to the top of the datagrid.
Here is my code
public void PriorityMoveTop()
{
Job? SelectedRow = (Job)this.jobListView.SelectedItem;
if (SelectedRow == null)
{
}
else
{
Job? myJobAfterSelectedRow = (from x in Memory.Jobs where x.Priority == (SelectedRow.Priority - 1) select x).FirstOrDefault();
if (jobListView.SelectedIndex == 0)
{
}
else
{
foreach (Operation operation in SelectedRow.Operations)
{
operation.Priority = SelectedRow.Priority;
}
OrderPriorityColumnDown();
SelectedRow.Priority = 1;
}
}
SetDataGridJobList();
if (SelectedRow == null)
{
}
else
{
jobListView.SelectedIndex = SelectedRow.Priority - 1;
}
}
The line
jobListView.SelectedIndex = SelectedRow.Priority - 1;
is where the row is supposed to highlight after the function has taken place. As i stated above this code works for moving a row up, down and to bottom. Any help on why this is not highlighting the row when moved to top?
Hi Alessandro Cirignaco,
We suspect that when the row is moved to the top of the SelectedRow.Priority value is zero. In this case, the SelectedIndex is set as -1. The selected row is not highlighted when the SelectedIndex value is -1 in SfDataGrid. This is the behavior of SfDataGrid. So, can you please check the SelectedIndex value when the row is moved to the top of the SelectedRow.Priority.
If you are still facing the same issue? Please provide more information related to your query?
1. Code snippet related to customization of OrderPriorityColumnDown, SetDataGridJobList, missed code in PriorityMoveTop method
2. Code snippet related to XAML and Code behind customization in SfDataGrid
3. If possible, kindly provide a simple issue reproducible sample
Kindly revert to us with the above requested details. It will be more helpful for us to check the possibilities to resolve the reported problem.
Regards,
Vijayarasan S
I have checked, once the line of code
jobListView.SelectedIndex = SelectedRow.Priority - 1;
has been stepped through the selected index becomes 0 and not -1.
void OrderPriorityColumnDown()
{
var records = jobListView.View.Records;
int rowCount = records.Count;
foreach (var record in records)
{
Job job = (Job)(record.Data);
if (job.Priority <= jobListView.SelectedIndex)
{
job.Priority++;
}
else
{
}
}
}
public void SetDataGridJobList()
{
jobListView.ItemsSource = null;
var ItemCollection = new ObservableCollection<Job>();
foreach (Job job in Memory.Jobs)
{
job.JobNo = job.JobNo;
job.Priority = job.Priority;
job.PartNo = job.PartNo;
job.PartDescription = job.PartDescription;
job.DateRequired = job.DateRequired;
job.SalesNo = job.SalesNo;
job.Customer = job.Customer;
job.Qty = job.Qty;
job.SaleValue = job.SaleValue;
job.ScheduledStartDate = job.ScheduledStartDate;
job.ScheduleCompleteDate = job.ScheduleCompleteDate;
job.OnTime = job.OnTime;
if (job.ScheduleCompleteDate > job.DateRequired)
{
job.OnTime = false;
}
else
{
job.OnTime = true;
}
ItemCollection.Add(job);
}
jobListView.ItemsSource = ItemCollection;
}
I have attached both methods that were missing from before
Hi Alessandro Cirignaco,
We regret to let you know that it is not possible to find the exact cause for
the reported issue without reproducing it on our end and only with the provided
details.
However, we have prepared the simple sample based on a provided code snippet
from our end. The reported issue is “Row selection does not select row when row is
moved to top of datagrid” and unable to replicate the issue from our end. It is
working fine as expected. Please find the tested sample and video in the
attachment.
If you still facing the same issue, provide more information related to your query?
1. Details about the code snippet related to customization when the row is moved to the top
2. Details about the code snippet related PriorityMoveTop method calling in your application
3. Video illustration of the issue
4. If
possible, kindly modify the sample based on your scenario.
Kindly revert to us with the above requested details. It
will be more helpful for us to check the possibilities to resolve the reported
problem.
Regards,
Vijayarasan S
I have it working with drag and drop. For some reason it does not work when i use a button click to take it to the top of the datagrid. I have checked the selectedIndex when the code is ran and it is 0, but the row does not highlight/selected.
The method MoveToTop is called from the buttonclick event to take the row to the top.
Hi Alessandro Cirignaco,
We regret to let you know that it is not possible to find the exact cause for
the reported issue without reproducing it on our end and only with the provided
details. We are in need of some information related to the reported scenario.
Provide more information related to your query?
Please revert to us with all the above requested details. It
will be helpful for us to check on it and provide you with the solution at the
earliest.
Regards,
Vijayarasan S
Hi,
I have modified the sample based on my scenario with any related code inside the sample project.
When you click to highlight a row and then press to top it will not reselect the top row.
Hi Alessandro Cirignaco,
We have checked the provided sample from our end. The reported problem occurs due to resetting the ItemsSource in SfDataGrid by calling the SetDataGridJobList method after the row is moved to Top or Bottom. In this method, initially you have tried to set the ItemsSource to null. So SelectedItems are not maintained in SfDataGrid.
However, you can overcome this behavior by maintaining the selection after reset the ItemsSource by using SelectedIndex like the code snippet mentioned below,
|
private void btnToTop_Click(object sender, RoutedEventArgs e) { PriorityMoveTop(); //here maintain the selected index in temporary variable var getSlectedIndex = jobListView.SelectedIndex; SetDataGridJobList(); //Afte the ItemsSource changed in SfDataGrid //here set the SelectedItemIndex jobListView.SelectedIndex = getSlectedIndex; }
private void btnBottom_Click(object sender, RoutedEventArgs e) { PriorityMoveBottom(); //here store the selected index in temporary variable var getSlectedIndex = jobListView.SelectedIndex; //in this method ItemsSource resetting after SetDataGridJobList(); //Afte the ItemsSource changed in SfDataGrid //here set the SelectedItemIndex jobListView.SelectedIndex = getSlectedIndex; } |
Please find the modified sample in the attachment and let us know if you have any concerns in this.
Regards,
Vijayarasan S
If this post is helpful, please consider Accepting it as the solution so that other members can locate it more quickly.
Hi Thank you for the reply.
In my actual project i have the buttons in the main window class and the datagrid as a User Control.
How would it be done from the MoveToTop() method
Edit: Thanks For this solution i managed to make it work
Hi Alessandro Cirignaco,
If you are satisfied with our response, please mark it as an answer. Otherwise,
please let us know if you have any further queries on this. We are happy to
help you😊.
Regards,
Vijayarasan S