Iterate all rows in GridView

8 posts, 0 answers
  1. Richard Harrigan
    Richard Harrigan avatar
    235 posts
    Member since:
    Nov 2009

    Posted 22 Oct 2013 Link to this post

    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

  2. Matt
    Matt avatar
    14 posts
    Member since:
    May 2012

    Posted 23 Oct 2013 Link to this post

    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...

  3. Ivan Ivanov
    Admin
    Ivan Ivanov avatar
    1218 posts

    Posted 23 Oct 2013 Link to this post

    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 >>
  4. Richard Harrigan
    Richard Harrigan avatar
    235 posts
    Member since:
    Nov 2009

    Posted 23 Oct 2013 Link to this post

    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


  5. Ed
    Ed avatar
    4 posts
    Member since:
    Feb 2014

    Posted 11 Feb 2014 in reply to Ivan Ivanov Link to this post

    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
  6. Dimitrina
    Admin
    Dimitrina avatar
    3769 posts

    Posted 13 Feb 2014 Link to this post

    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).

  7. Olatunde
    Olatunde avatar
    1 posts
    Member since:
    Dec 2011

    Posted 26 Jun 2014 Link to this post

    How to add row to a datagridview dynamically in WPF
  8. Matt
    Matt avatar
    14 posts
    Member since:
    May 2012

    Posted 26 Jun 2014 in reply to Olatunde Link to this post

    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...
Back to Top