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

Observable Collections and SelectionChangeEventArgs

1 Answer 114 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Jonathan
Top achievements
Rank 1
Jonathan asked on 06 Sep 2011, 11:15 PM
I have a list of zipcode objects that I bind to a datagridview based on a selection from a treeview, the treeview is unimportant other than only a subset of available zipcodes are bound to the grid at any one time.

GridSelectedZipCodes.ItemsSource = ZipGeoCodeService.GetForZipPart((string)item.Header, (float)_selectedDealer.Latitude, (float)_selectedDealer.Longitude, (float)_selectedDealer.MaxDistance);

where GetForZipPart has a function signature of:

static public List<ZipGeoCode> GetForZipPart(string zippart, float latitude, float longitude, float distance)

I want to select items in the gridview based upon another list of ZipGeoCode objects that correspond to a dealer.  The dealer list may contain zipcodes from other zipparts that are not bound to the grid.  I do the selection like so:

private void SelectZipCodes()
{
    IsLoading = true;
    foreach (ZipGeoCode _item in GridSelectedZipCodes.Items)
    {
        if (_dealerZipCodes.Find(delegate(ZipGeoCode zgc) {return zgc.ID == _item.ID; }) != null)
        {
            GridSelectedZipCodes.SelectedItems.Add(_item);
        }
    }
    IsLoading = false;
}


so far so good, however now when an item is selected or deselected in the radgridview I want to add or remove it from the _dealerZipCodes in the most efficient manner.  I suspect i shoudl use the radgrdiview_selectionchanged event.  I am wondering if ther is a way to do this with observable collections?.  Any suggestions greatly appreciated and info how I can act only on the item(s) that were selected/deselected by accessing the

SelectionChangeEventArgs

would be great.

I'm not using MVVM.

Currently my radgrdiview_selectionchanged looks like the following:

private void GridSelectedZipCodes_SelectionChanged(object sender, SelectionChangeEventArgs e)
       {
           if (!(IsLoading))
           {
               //remove all items
               foreach (ZipGeoCode _item in GridSelectedZipCodes.Items)
               {
                   if (_dealerZipCodes.Find(delegate(ZipGeoCode zgc) { return zgc.ID == _item.ID; }) != null)
                   {
                       _dealerZipCodes.Remove(_item);
                   }
               }
               foreach (ZipGeoCode _item in GridSelectedZipCodes.SelectedItems)
               {
                   _dealerZipCodes.Add(_item);
               }
           }
       }

Not very efficient.

Thanks for the help/direction.

For a visual of what I'm doing: http://www.metamorpho-sys.com/telerik/Capture.GIF

Jonathan


1 Answer, 1 is accepted

Sort by
0
Maya
Telerik team
answered on 08 Sep 2011, 10:09 AM
Hello Jonathan,

I would suggest to bind the tree list view - DealerZipCodes - directly to the SelectedItems of RadGridView. Please take a look at this blog post for a reference on a similar scenario. Does it correspond to your requirements ? 
 

Greetings,
Maya
the Telerik team

Thank you for being the most amazing .NET community! Your unfailing support is what helps us charge forward! We'd appreciate your vote for Telerik in this year's DevProConnections Awards. We are competing in mind-blowing 20 categories and every vote counts! VOTE for Telerik NOW >>

Tags
GridView
Asked by
Jonathan
Top achievements
Rank 1
Answers by
Maya
Telerik team
Share this question
or