locked
Re-ordering Gridview Items programmatically

    Question

  • Hi,

    I have seen a couple of articles/tutorials on setting up the GridView to re-order its items by using drag and drop. I need to be able to move an item to a new location in the grid (and have the rest of the items reorder) by using the arrow keys on my keyboard. What methods are available to me for this?

    thnx

    Brian

    Tuesday, December 9, 2014 7:09 PM

Answers

  • Hi Brian,

    Based on my knowledge, I’ve not seen such methods to reorder the GridView items. As you have mentioned, you wanted to reorder the GridView items programmatically, I would recommend you try the following approach.

    Add an index property to the GridView item class, when you want to reorder the GridView items. Just adjust the value of this property of the data source array and then rebind them to GridView. Some code snippet looks like.

    Item class.

    public class Item : System.ComponentModel.INotifyPropertyChanged
        {
            public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;
            protected virtual void OnPropertyChanged(string propertyName)
            {
                if (this.PropertyChanged != null)
                {
                    this.PropertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs(propertyName));
                }
            }
            private string _Title = string.Empty;
            public string Title
            {
                get
                {
                    return this._Title;
                }
                set
                {
                    if (this._Title != value)
                    {
                        this._Title = value;
                        this.OnPropertyChanged("Title");
                    }
                }
            }
           //other properties here
            public int Order { get; set; }
    } 
    

    Adjust the order of data source programmatically.

    private void Button_Click(object sender, RoutedEventArgs e)
            {
                Random rnd=new Random();
                // create a new instance of store data
                storeData = new StoreData();
                for (int i = 0; i < storeData.Collection.Count; i++)
                {
                    storeData.Collection[i].Order = rnd.Next(0, 1001);
                }
                var data = storeData.Collection.OrderBy(item => item.Order);
                // set the source of the GridView to be the sample data
                ItemGridView.ItemsSource = data;
            }
    

    I modify the MSDN sample code, you can refer to the following link to retrieve it. https://code.msdn.microsoft.com/windowsapps/XAML-ListView-and-GridView-6bd77f71/view/SourceCode#content.

    Regards,


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place. Click HERE to participate the survey.

    Wednesday, December 10, 2014 8:06 AM
    Moderator