How to change cell values on drop event

Hi,


I have a datagrid that currently displays seeded data.

The datagrid is ordered by a column I have called priority which is just numbers (1, 2 ,3 ,4 etc).

What I am trying to achieve is if i drag the row which has a priority of 3 and drop it on top of row which has a priority of 1, changes priority 3 to 1 and then loops through the other rows to order them accordingly (2, 3, 4).

So my final outcome after drop event would have the priority column like 1, 2, 3, 4.

private void jobListView_Drop(object sender, DragEventArgs e)

        {

            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 if(job.Priority > jobListView.SelectedIndex)

                {

                    job.Priority--;

                }

            }

        }


The code above does not work but this was my starting point and i am not sure how else to go about it


3 Replies 1 reply marked as answer

VS Vijayarasan Sivanandham Syncfusion Team July 25, 2022 03:03 PM UTC

Hi Alessandro Cirignaco,

Your requirement to maintain the priority column (ex: 1, 2, 3, 4) while drag drop the records on the top in SfDataGrid can be achieved by customizing the Drop event. Please refer to the below code snippet,

private void OnDrop(object sender, DragEventArgs e)

{

            var records = jobListView.View.Records;

           

            foreach (var record in records)

            {

                OrderInfo job = (OrderInfo)(record.Data);

 

                //here get the row index of the record

                var rowIndex = jobListView.ResolveToRowIndex(record.Data);

 

                //here get the recordInde index of record

                var recordIndex = jobListView.ResolveToRecordIndex(rowIndex);

 

                //record index starting from 0. So, we need set the plus one value for all records

                //here set the value for Priority column

                job.Priority = recordIndex + 1;         

 

            }

}


Please find the sample in the attachment.

UG Link: https://help.syncfusion.com/wpf/datagrid/helpers

If we misunderstood your requirement, please provide more information regarding the requirement. This would help us to proceed further.

Regards,

Vijayarasan S


Attachment: Sample_35a6b9f5.zip

Marked as answer

AC Alessandro Cirignaco July 26, 2022 08:14 AM UTC

Hi Vijayarasan,


Thank you for the response. From what i can see in the backend this seems to work. However it is not updating on the datagrid front end.

Can the Datagrid be refreshed?


I managed to get it to work, Thanks for your help



VS Vijayarasan Sivanandham Syncfusion Team July 26, 2022 01:55 PM UTC

Hi Alessandro Cirignaco,


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. We are happy to help you😊.


Regards,

Vijayarasan S







Loader.
Up arrow icon