The EntitySpaces Community

Share and learn about the EntitySpaces Architecture.
Welcome to The EntitySpaces Community Sign in | Join | Help
in
Home Forums Photos

Need to reorder collection of entities

Last post 03-01-2007, 5:50 PM by David.Parsons. 5 replies.
Sort Posts: Previous Next
  •  02-28-2007, 11:54 AM 643

    Need to reorder collection of entities

    I have an entity, "IFProductProtocol" which has the following attributes:

    ID (PK)
    Name
    OrderIndex //  this is used to specify the order in which the data should be processed

    I have the collection bound to a DataGridVew and I want to reorder them via drag and drop in the DGV.  I've read that in order to change the order of items in a DataBound DGV, you need to change the order in the underlying data source (makes sense).  I've looked over the esCollection methods and don't see anything for InsertAt(), RemoveAt(), etc.

    I'm really not sure how to approach this...

    One idea I had is when a user drags and drops a DatagridRow to call a method that would go through the collection and manually update the OrderIndex property for the effected entities, then sort the collection on OrderIndex.  I think this will do the trick.

    If anyone has any other ideas I would love to hear them.

    Thanks,
    Steve
     

  •  02-28-2007, 1:42 PM 654 in reply to 643

    Re: Need to reorder collection of entities

    The underlying collection can only be sorted by one of the columns in the collection. I think updating OrderIndex is your best option.
    David Neal Parsons
    www.entityspaces.net
  •  02-28-2007, 6:53 PM 670 in reply to 643

    Re: Need to reorder collection of entities

    **  For some reason segments[ i ] is printing a lightbulb icon? ** 

    HI David, Thanks for the confirmation.

    I'm implemented a quick and dirty sort method

    Code:
    1    public void ReorderSegment(IFProtocolSegment movingSegment, IFProtocolSegment targetSegment)
    2 {
    3 // The user dragged a segment to a different location in the DGV.
    4 // Adjust the OrderIndex property for all of the segments to reflect
    5 // this change. The DGV's DataSource is sorted on the OrderIndex, so
    6 // changing the DataSource should update the grid with changes.
    7 int startIndex = Math.Min(movingSegment.Index.Value, targetSegment.Index.Value);
    8 int endIndex = Math.Max(movingSegment.Index.Value, targetSegment.Index.Value);
    9 bool movingUp = (movingSegment.Index.Value > targetSegment.Index.Value);
    10 IFProtocolSegmentCollection segments = _activeProtocol.IFProtocolSegmentCollectionByProtocolID;
    11 12 short targetIndex = targetSegment.Index.Value;
    13 14 for (int i = startIndex; i < endIndex; i++)
    15 {
    16 if(movingUp)
    17 {
    18 (segmentsIdea as IFProtocolSegment).Index++;
    19 }
    20 else 21 {
    22 (segmentsIdea as IFProtocolSegment).Index--;
    23 }
    24 }
    25 26 movingSegment.Index = targetIndex;
    27 segments.Sort = "Index ASC";
    28 }

     It seems to do the trick OK.  The problem I'm having now is that the DataGridView doesn't update unless I click on cells and even then it only updates the row I've clicked on (efficient, good for them!) - my question is, what is the best way to tell the DGV "Hey, redraw everything!" - is the best way setting the DataSource null then back to the entity collection again?

    No big deal, you seem to know a lot about the DGV so I figured I'd ask.

    -Steve 

  •  02-28-2007, 10:59 PM 671 in reply to 670

    Re: Need to reorder collection of entities

    dataGridView1.Refresh();
    David Neal Parsons
    www.entityspaces.net
  •  03-01-2007, 5:36 AM 674 in reply to 671

    Re: Need to reorder collection of entities

    Refresh() didn't have any effect, I thought it would as well, but nothing.

    What ended up working is adding this line to the end of my ReorderSegment method

    Code:
    1    movingSegment.EndEdit();

    I don't fully understand why, but I'm guessing that EndEdit is notifying the grid the data has changed?
     

  •  03-01-2007, 5:50 PM 696 in reply to 674

    Re: Need to reorder collection of entities

    Right... Since you are changing the underlying collection and not rows in the DGV, entity.EndEdit() should do the trick. I think you can also call bindingSource1.ResetBindings(false) to push changes back up to the DGV from a bound collection.
    David Neal Parsons
    www.entityspaces.net
View as RSS news feed in XML