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

Iterate all rows in GridView

7 Answers 320 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Richard Harrigan
Top achievements
Rank 1
Richard Harrigan asked on 22 Oct 2013, 07:48 PM
Hi,

I need to iterate all rows in the grid under for the following criteria:

Virtual Rows and Columns are enabled 
I am using GridView filter capabilities to reduce number of visible rows

I know from reading similar forum responses that it is strongly recommended not to iterate the grid rows but iterate thru the bound data object.  However, the data object does not reflect the filtering on the data as displayed in the grid.   In my app I need to save the data (no filtered rows) in a custom format to a file.  How do you recommend I do this?

Since the GridView software is aware of virtual rows and is also aware of what is filtered a new GridView method to iterate the data object would be very helpful.  This method would take into account the filtering as well as columns hidded by a column chooser.

Thanks
Rich

7 Answers, 1 is accepted

Sort by
0
Matt
Top achievements
Rank 1
answered on 23 Oct 2013, 02:10 PM
Why not make a dependency property that is of the type that the ItemsSource of the grid is and whenever the 'Items' collection changes (which will be changed whenever filtering occurs I believe) update that collection and then you will have a collection on your (in most cases ViewModel) which will have the filtered subset of all items in the grid?

Something like:
  private readonly RadGridView _radGridView;
 
        public SetRadGridViewItems(RadGridView radGridView)
        {
            this._radGridView = radGridView;
        }
 
public static readonly DependencyProperty ItemsProperty =
            DependencyProperty.RegisterAttached("Items",
                typeof(SmartObservableCollection<T>),
                typeof(SetRadGridViewItems),
                new PropertyMetadata(new PropertyChangedCallback(OnItemsPropertyChanged)));
 
        public static void SetItems(DependencyObject dependencyObject, SmartObservableCollection<T> items)
        {
            dependencyObject.SetValue(ItemsProperty, items);
        }
 
        public static SmartObservableCollection<T> GetItems(DependencyObject dependencyObject)
        {
            return (SmartObservableCollection<T>)dependencyObject.GetValue(ItemsProperty);
        }
 
        private static void OnItemsPropertyChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e)
        {
            if (e.NewValue != null)
            {
                SetItems(dependencyObject, e.NewValue as SmartObservableCollection<T>);
            }
     }
 
        private void Attach()
        {
            _radGridView.Items.CollectionChanged += Items_CollectionChanged;
        }
 
        private void Items_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            SmartObservableCollection<T> data = this._radGridView.GetValue(ItemsProperty) as SmartObservableCollection<T>;
 
            data.Clear();
 
            data.DisableNotifyCollectionChanged();
 
            foreach (dataItem obj in _radGridView.Items)
            {
                data.Add(obj);
            }
 
            data.EnableNotifyCollectionChanged();
        }
In the XAML then you have:
<telerik:RadGridView (various properties) local:SetRadGridViewItems.Items="{Binding MyCollectionToBindTo, Mode=TwoWay}"/>
Or something of that nature.

Should do the trick I think...Could be an easier solution but I think this will give you the desired subset of data.

The saving of data could be pretty easy using some of the export functionality that the controls have baked in.  Just check out the documentation there, though I prefer to use something like ClosedXML for my exporting needs.

**Edit**
Sorry I think I misread what you are trying to do there...my bad...

0
Ivan Ivanov
Telerik team
answered on 23 Oct 2013, 03:29 PM
Hello,

 You can use a QueryableCollectionView as an ItemsSource value for RadGridView. In this way it will automatically sync its sort, filter and group descriptors with RadGridView and you will be able to track the changes in your view model. I am attaching a sample project that illustrates this approach.

Regards,
Ivan Ivanov
Telerik
TRY TELERIK'S NEWEST PRODUCT - EQATEC APPLICATION ANALYTICS for WPF.
Learn what features your users use (or don't use) in your application. Know your audience. Target it better. Develop wisely.
Sign up for Free application insights >>
0
Richard Harrigan
Top achievements
Rank 1
answered on 23 Oct 2013, 07:59 PM
Hi Ivan,

Loading ItemsSource with a QueryableCollectionView object worked great. It is exactly what I needed and couldn't have been easier.

Many Thanks
Rich


0
Ed
Top achievements
Rank 1
answered on 11 Feb 2014, 06:13 AM
Hello,

Are you saying that the change in the GridView will be reflected in the source, i.e., when I'm sorting in GridView, that the source will also be sorted in the same order?

Regards,

Ed
0
Dimitrina
Telerik team
answered on 13 Feb 2014, 02:15 PM
Hello,

What my colleague explained is that the SortDescriptors between RadGridView and QueryableCollectionView will be synchronized. The data displayed in RadGridView will be sorted based on the active SortDescriptors, not the source collection itself.

Regards,
Didie
Telerik

Check out the new Telerik Platform - the only modular platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native apps. Register for the free online keynote and webinar to learn more about the Platform on Wednesday, February 12, 2014 at 11:00 a.m. ET (8:00 a.m. PT).

0
Olatunde
Top achievements
Rank 1
answered on 26 Jun 2014, 06:50 PM
How to add row to a datagridview dynamically in WPF
0
Matt
Top achievements
Rank 1
answered on 26 Jun 2014, 06:52 PM
If your bindings are in place, just adding an item to the collection as the result of whatever your add event is, should add the row...
Tags
GridView
Asked by
Richard Harrigan
Top achievements
Rank 1
Answers by
Matt
Top achievements
Rank 1
Ivan Ivanov
Telerik team
Richard Harrigan
Top achievements
Rank 1
Ed
Top achievements
Rank 1
Dimitrina
Telerik team
Olatunde
Top achievements
Rank 1
Share this question
or