Quantcast
Channel: Telerik Forums RSS
Viewing all articles
Browse latest Browse all 94857

Binding a subset of columns from a Datatable to RadGridView

$
0
0

Sure, I have a datatable with some number of columns.  I am using the first column as the X on a graph, and each of the other columns represent a curve in the Y)

Each of the other columns mapped to controls which when selected SHOULD change the datagrid to display the first column and which ever other column is associated with that control.  (The graph shows the other column in the Y on the graph)  So when I select the control the view and viewmodel are set and bound, but during the binding I need to be able to only show the specified columns. 

<telerik:RadGridViewx:Name="dataGrid"  ItemsSource="{Binding Table.DefaultView}"AutoGenerateColumns="false"AllowDrop="True"CanUserDeleteRows="True"AlternateRowBackground="#FFCDCDCD"CanUserInsertRows="True"SelectionMode="Extended"SelectionUnit="Cell"AlternationCount="2"ClipToBounds="True"AutoExpandGroups="True"ClipboardCopyMode="All"ClipboardPasteMode="AllSelectedCells">
        <i:Interaction.Behaviors>
            <local:ColumnBindingBehaviorColumns="{Binding Columns}"/>
        </i:Interaction.Behaviors>
 </telerik:RadGridView.ContextMenu>

ColumnBindingBehaviour is similar to a previous post

 

publicclassColumnBindingBehavior : Behavior<RadGridView>
    {
        privateRadGridView Grid
        {
            get
            {
                returnAssociatedObject;
            }
        }
 
        publicINotifyCollectionChanged Columns
        {
            get{ return(INotifyCollectionChanged)GetValue(ColumnsItemsProperty); }
            set{ SetValue(ColumnsItemsProperty, value); }
        }
 
        publicstaticreadonlyDependencyProperty ColumnsItemsProperty =
            DependencyProperty.Register("Columns", typeof(INotifyCollectionChanged), typeof(ColumnBindingBehavior), newPropertyMetadata(OnColumnsPropertyChanged));
 
 
        privatestaticvoidOnColumnsPropertyChanged(DependencyObject target, DependencyPropertyChangedEventArgs args)
        {
            var behavior = (ColumnBindingBehavior)target;
            var collection = args.NewValue asINotifyCollectionChanged;
            if(collection != null)
            {
                collection.CollectionChanged -= behavior.ContextColumns_CollectionChanged;
                collection.CollectionChanged += behavior.ContextColumns_CollectionChanged;
            }
        }
 
        protectedoverridevoidOnAttached()
        {
            base.OnAttached();
 
            Transfer(Columns asIList, Grid.Columns);
 
            Grid.Columns.CollectionChanged -= GridColumns_CollectionChanged;
            Grid.Columns.CollectionChanged += GridColumns_CollectionChanged;
        }
 
        voidContextColumns_CollectionChanged(objectsender, NotifyCollectionChangedEventArgs e)
        {
            UnsubscribeFromEvents();
 
            Transfer(Columns asIList, Grid.Columns);
 
            SubscribeToEvents();
        }
 
        voidGridColumns_CollectionChanged(objectsender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            UnsubscribeFromEvents();
 
            Transfer(Grid.Columns, Columns asIList);
 
            SubscribeToEvents();
        }
 
        privatevoidSubscribeToEvents()
        {
            Grid.Columns.CollectionChanged += GridColumns_CollectionChanged;
 
            if(Columns != null)
            {
                Columns.CollectionChanged += ContextColumns_CollectionChanged;
            }
        }
 
        privatevoidUnsubscribeFromEvents()
        {
            Grid.Columns.CollectionChanged -= GridColumns_CollectionChanged;
 
            if(Columns != null)
            {
                Columns.CollectionChanged -= ContextColumns_CollectionChanged;
            }
        }
 
        publicstaticvoidTransfer(IList source, IList target)
        {
            if(source == null|| target == null)
                return;
 
            target.Clear();
 
            foreach(var o insource)
            {
                target.Add(o);
            }
        }
    }

 

If the autogeneratecolumns is on I get all of them, if off I get nothing.

There is something Im doing wrong but I don't know what.

 


Viewing all articles
Browse latest Browse all 94857

Latest Images

Trending Articles



Latest Images