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

WPF GridView DisplayOrder

6 Answers 66 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Joshua
Top achievements
Rank 1
Joshua asked on 22 May 2013, 02:45 AM
I have a GridView, it supports drag and drop to reorder the rows.  It is bound to a custom object.  When the user updates the grid and saves it I would like to save the Row's Index to a property on the object called DisplayOrder.  Is it possible to bind the RowIndex to the DisplayOrder Property on the object that is the DataContext for the grid?

6 Answers, 1 is accepted

Sort by
0
Rossen Hristov
Telerik team
answered on 22 May 2013, 11:43 AM
Hello,

The "RowIndex" as you call it, is the index of the respective business object instance in your source collection that is fed to RadGridView.ItemsSource.

Regards,
Rossen Hristov
Telerik

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

0
Joshua
Top achievements
Rank 1
answered on 22 May 2013, 11:49 AM
So does this mean when using the GridView Reorder Rows example when the items are moved within the grid using the drag and drop, is the collection updated so that is index now reflects its new position?  So instead of being able to bind I will have to iterate through the collection and update my property with the index of each item?
0
Accepted
Rossen Hristov
Telerik team
answered on 22 May 2013, 12:05 PM
Hello,

That is correct.

In fact, row re-ordering is not even an out-of-box functionality provided by the grid. It is custom code in an online example, i.e. it is source code that could be (should be) written by you. You can see what the actual source code does for row reordering in the example where you found row reordering in the first place:

private void OnDrop(object sender, Telerik.Windows.DragDrop.DragEventArgs e)
        {
            var draggedItem = DragDropPayloadManager.GetDataFromObject(e.Data, "DraggedItem");
            var details = DragDropPayloadManager.GetDataFromObject(e.Data, "DropDetails") as DropIndicationDetails;
 
            if (details == null || draggedItem == null)
            {
                return;
            }
 
            if (e.Effects == DragDropEffects.Move || e.Effects == DragDropEffects.All)
            {
                ((sender as RadGridView).ItemsSource as IList).Remove(draggedItem);
            }
 
            if (e.Effects != DragDropEffects.None)
            {      
                var collection = (sender as RadGridView).ItemsSource as IList;
                int index = details.DropIndex < 0 ? 0 : details.DropIndex;
                index = details.DropIndex > collection.Count - 1 ? collection.Count : index;
 
                collection.Insert(index, draggedItem);         
            }
        }

Please, try to understand what this source code does and that will answer your questions.

If you want to have property that shows the current index of an object, if I were you I would simply pass the instance of the parent collection to my business object constructor and then create something like this:

public int Index
{
get
{
return this.parentCollection.IndexOf(this);
}
}

The magic method here is the IndexOf method, which does what its name says.

Of course, there are hundreds of other ways to implement this, but this is general .NET stuff and I will leave them to your imagination.

I hope this helps.

Regards,
Rossen Hristov
Telerik

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

0
Joshua
Top achievements
Rank 1
answered on 22 May 2013, 07:11 PM
Thank you for your help.  One question, in the sample code I could not find the method for OnDrop, can you point me in the direction of where the sample of that code is?  Also, the tool tip that pops up when you are dragging the items, I found the part where I can update the second line (Drop Before So and So, Drop After So and So, etc) (under OnGridViewRowDropQuery), but I cannot seem to track down where the first line is created.  In the sample it is worded as Customer: So and So, on mine it puts the type of the object, but I cannot find where to modify this...
0
John
Top achievements
Rank 1
answered on 23 May 2013, 03:03 AM
I think I just realized what part of my problem is.  I think I am using an older version of Telerik from what the samples are done in....
0
Rossen Hristov
Telerik team
answered on 27 May 2013, 07:37 AM
Hello,

An example of row reordering can be seen in our online Quick Start Framework located here. The name of the example is Row Reorder. I have attached a zip file with the source code files. The sample data is created in another class which is common for many samples and that is why is not in the sample source code.

You should be able locate all of the source code in your local installation of our Quick Start Framework.

I hope this helps.

Regards,
Rossen Hristov
Telerik

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

Tags
GridView
Asked by
Joshua
Top achievements
Rank 1
Answers by
Rossen Hristov
Telerik team
Joshua
Top achievements
Rank 1
John
Top achievements
Rank 1
Share this question
or