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

How to return set values of a selected row

1 Answer 69 Views
GridView
This is a migrated thread and some comments may be shown as answers.
VisionAIR
Top achievements
Rank 2
VisionAIR asked on 16 Jun 2010, 04:24 PM
Within a project I have a grid bound to a Index collection which is generated from a proxy service
 private void QueryActivities()  
        {  
            QueryClient qc = new QueryClient("BasicHttpBinding_IQuery");  
            QueryFilter filter = new QueryFilter();  
            filter.CallForService = cfsCB;  
            filter.NCICReturn = false;  
            filter.RMSQuery = false;  
            filter.DLSwipe = false;  
            filter.Booking = false;  
            filter.ArrestReport = reportCB;  
            filter.CustodyReport = reportCB;  
            filter.CitationReport = reportCB;  
            filter.FieldInterviewReport = reportCB;  
            filter.IncidentReport = reportCB;  
            filter.IncidentSupplimentReport = reportCB;  
            var actresult = qc.GetFilteredActivityIndex(filter);  
              
            this.actGridView.ItemsSource = actresult;  
 
        } 
The grid currentlty displays 5 (of 20) fields ( first name , last name, activity type, Address, and date )

I need to be able to select a row and return an assoicated ID field which is part of the index collection. ID is an actual object and not the index of [0]... of the colleciton or row.

Any samples or suggestions would be appreciated.

In the past I have seen examples such as

//placed in Window initialization  
 
this.actGridView.Loaded += (o, e) => 
          {  
              this.actGridView.SelectedItems.CollectionChanged += new NotifyCollectionChangedEventHandler(SelectedItems_CollectionChanged);  
          }; 
void SelectedItems_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)  
        {  
            if (e.Action == NotifyCollectionChangedAction.Add)  
            {  
                var person = e.NewItems[0] as People;  
                //PeopleView.SelectedPeople.Add(person); //writes selected values to selected people when row is selected.  
                myID = person.uniqueID;  
            }  
        } 
Where this has not worked is that I only want to captrure one value I do not want to recreate or build a new collection . Also using this approach the variable is attempted to be set when the grid loads however it is currently setting it to null or no value so I assume something is amiss either in the code or the approach I am taking.

Any suggestions on how to capture just one value of the collection which is not visually rendered in the grid vs rebuilding an entire collection would be greatly appreciated.

I am currently using the WPF desktop UI controls Q2 2010 ( 2010.1.603.35 )

Thank you
Randy

1 Answer, 1 is accepted

Sort by
0
Accepted
Yavor Georgiev
Telerik team
answered on 17 Jun 2010, 08:59 AM
Hi Randall Crews,

 If you have a GridViewRow object, you can access the data items it represents by getting the value of the Item property. You can then work with this object as you see fit.

If you wish to access the data object when a row is selected by the user, you can do something like this:

this.gridView.SelectionChanged += new EventHandler<SelectionChangeEventArgs>(gridView_SelectionChanged);
void gridView_SelectionChanged(object sender, SelectionChangeEventArgs e)
{
    foreach (BusinessObject item in e.AddedItems)
    {
        MessageBox.Show("You have selected an item with Id: " + item.ID.ToString());
    }
}

Because the RadGridView supports selecting multiple items at once, you are presented with a collection of selected items, rather than a single item. In the SelectionChanged event handler you can invoke whatever business logic you need to operate on your data objects' properties.

Also, regarding SelectedItems ObservableCollection's CollectionChanged event, it fires once when the GridView is created with no items at all. This is normal and is part of the type's behavior. Subsequent instances of this event will contain the expected data about what items were added or removed.

Kind regards,
Yavor Georgiev
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
Tags
GridView
Asked by
VisionAIR
Top achievements
Rank 2
Answers by
Yavor Georgiev
Telerik team
Share this question
or