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

[Solved] Reorder Rows - Drag and Drop

4 Answers 193 Views
GridView
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
UGH!!
Top achievements
Rank 1
UGH!! asked on 01 Dec 2010, 10:28 PM

Following this example exactly . . .

http://localhost:65230/Default.aspx#GridView/RowReorder

Object reference not set to an instance of an object on (arrowed below) . . . In debugging, source is always null.

<telerik:RadGridView  x:Name="_radGridView1"
                                   ItemsSource="{Binding Data, ElementName=_supplyDomainDataSource}"
                                   IsReadOnly="True"
                                   ShowGroupPanel="False"
                                   AllowDrop="True"
                                   HorizontalAlignment="Stretch" VerticalAlignment="Stretch"
                                   AutoGenerateColumns="False" 
                                   SelectionMode="Extended" ScrollMode="Deferred">


 

private void OnDragInfo(object sender, DragDropEventArgs e)
        {
            if (!(e.Options.Source is GridViewRow))
            {
                return;
            }
  
            RadGridView gridView = sender as RadGridView;
            IEnumerable draggedItems = e.Options.Payload as IEnumerable;
  
            if (e.Options.Status == DragStatus.DragInProgress)
            {
                this.dragCue = this.CreateDragCue();
                this.dragCue.ItemsSource = draggedItems;
  
                e.Options.DragCue = this.dragCue;
  
            }
            else if (e.Options.Status == DragStatus.DragComplete)
            {
                IList source = gridView.ItemsSource as IList; <<<--------------------
  
                foreach (object draggedItem in draggedItems)
                {
                    source.Remove(draggedItem);
                }
            }
        }
  
        private void OnGridViewRowDropQuery(object sender, DragDropQueryEventArgs e)
        {
            if (!(e.Options.Source is GridViewRow))
            {
                return;
            }
  
            GridViewRow row = sender as GridViewRow;
  
            if (e.Options.Status == DragStatus.DropDestinationQuery)
            {
                this.currentDropItem = row.Item;
                this.currentDropPosition = this.GetDropPositionFromPoint(e.Options.CurrentDragPoint, row);
  
                e.QueryResult = true;
                e.Handled = true;
            }
        }

 

 


HELP!!

 

4 Answers, 1 is accepted

Sort by
0
Tsvyatko
Telerik team
answered on 02 Dec 2010, 09:36 AM
Hi UGH!!,

In the code posted I can see that you are using DomainDataSource.Data as ItemsSource.

The reason you are getting null reference is that DomainDataSource.Data does not have defined items order (It implements IEnumerable but not IList). As a result you cannot reorder items. You might want to define custom field in your data model e.g. OrderID and modify the order logic to rely on this data.

Best wishes,
Tsvyatko
the Telerik team
Browse the videos here>> to help you get started with RadControls for Silverlight
0
UGH!!
Top achievements
Rank 1
answered on 02 Dec 2010, 05:23 PM
Thanks for the reply!!  Can you post an example for me to pick at??
0
UGH!!
Top achievements
Rank 1
answered on 02 Dec 2010, 08:38 PM
What is the preferred binding method for drag and drop within an RadGridView??
0
UGH!!
Top achievements
Rank 1
answered on 06 Dec 2010, 02:27 AM
Having no response from Telerik and spending many hours on this . . . .

Here is the solution I found to work. Convert the entity to an observablecollection on the LoadedData event of the DomainDataSource.  Like this . . 

private ObservableCollection<Product_Category> myProdCategory = new ObservableCollection<Product_Category>(); 
  
private void product_CategoryDomainDataSource_LoadedData(object sender, LoadedDataEventArgs e)
        {
  
            if (e.HasError)
            {
                System.Windows.MessageBox.Show(e.Error.ToString(), "Load Error", System.Windows.MessageBoxButton.OK);
                e.MarkErrorAsHandled();
            }
            {               
                var productCategories = e.Entities;
                foreach (Product_Category dbProductCategory in productCategories)
                {
                    Product_Category newProductCategory = new Product_Category();
                    newProductCategory.Category_Id = dbProductCategory.Category_Id;
                    newProductCategory.Category_Name = dbProductCategory.Category_Name;
  
                    myProdCategory.Add(newProductCategory);
                    DG.ItemsSource = myProdCategory;
                }
            }
        }

 

 

 

 

 

I also found I could just hook it like the below and it would convert it to the IList collection so "source" would not be null, but would error OnDragInfo would error stating "Collection is read-only."

 

DG.ItemsSource = e.entities;

I hope this helps save someone loads of time.

 

Tags
GridView
Asked by
UGH!!
Top achievements
Rank 1
Answers by
Tsvyatko
Telerik team
UGH!!
Top achievements
Rank 1
Share this question
or