Welcome to the Xceed Community | Help
Community Search  
More Search Options

Dynamic Expand/Collapse

Sort Posts: Previous Next
  •  09-01-2010, 8:08 AM Post no. 28543

    Dynamic Expand/Collapse

    Hi,

        In my application i want to do expand/collapse dynamically. how i can achieve.

    Description: i have Collapse and Expand button. if i click Collapse button, the grid should be collapse. if i click Expand the grid should be expand. below i gave my sample code. please help me.

    XAML

    -------- 

    <Window x:Class="GroupingDemo.Window1"

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    Title="Window1" Height="300" Width="300" xmlns:xcdg="http://schemas.xceed.com/wpf/xaml/datagrid">

    <Grid Name="dataGrid">

    <Grid.RowDefinitions>

    <RowDefinition Height="50"/>

    <RowDefinition Height="*"/>

    </Grid.RowDefinitions>

    <Button Name="btnExpand" Content="Expand" Click="btnExpand_Click" HorizontalAlignment="Left" Width="121" />

    <Button Name="btnCollapse" Content="Collapse" Click="btnCollapse_Click" HorizontalAlignment="Right" Width="121" />

    <xcdg:DataGridControl Margin="0,12,-2,90" Name="dataGridControl1" Grid.Row="1">

    <!--<xcdg:DataGridControl.DefaultGroupConfiguration>

    <xcdg:GroupConfiguration InitiallyExpanded="False"/>

    </xcdg:DataGridControl.DefaultGroupConfiguration>-->

    <xcdg:DataGridControl.View>

    <xcdg:TableView />

    </xcdg:DataGridControl.View>

    <xcdg:DataGridControl.Columns>

    <xcdg:Column FieldName="Category" Title="Category" />

    <xcdg:Column FieldName="Product" Title="Product" />

    <xcdg:Column FieldName="UnitPrice" Title="UnitPrice" />

    </xcdg:DataGridControl.Columns>

    </xcdg:DataGridControl>

    </Grid>

    </Window>

     

     

     

    Code behind

    ---------------------

    using System;

    using System.Collections.Generic;

    using System.Linq;

    using System.Text;

    using System.Windows;

    using System.Windows.Controls;

    using System.Windows.Data;

    using System.Windows.Documents;

    using System.Windows.Input;

    using System.Windows.Media;

    using System.Windows.Media.Imaging;

    using System.Windows.Navigation;

    using System.Windows.Shapes;

    using System.Data;

    using Xceed.Wpf.DataGrid;

    using Xceed.Wpf.DataGrid.Views;

    namespace GroupingDemo

    {

    /// <summary>

    /// Interaction logic for Window1.xaml

    /// </summary>

    public partial class Window1 : Window

    {

    DataTable dtProducts = new DataTable("Products");

    DataGridCollectionViewSource objDataGridCollectionViewSource = null;

    GroupConfiguration objGroup = new GroupConfiguration();

     

     

    public Window1()

    {

    InitializeComponent();

    objDataGridCollectionViewSource = new DataGridCollectionViewSource();

     

    dtProducts.Columns.Add("Category", typeof(string));

    dtProducts.Columns.Add("Product", typeof(string));

    dtProducts.Columns.Add("UnitPrice", typeof(double));

    dtProducts.Rows.Add(new object[] {"C001","P001",150 });

    dtProducts.Rows.Add(new object[] { "C001", "P002", 1350 });

    dtProducts.Rows.Add(new object[] { "C001", "P003", 12150 });

    dtProducts.Rows.Add(new object[] { "C002", "P006", 154230 });

    dtProducts.Rows.Add(new object[] { "C002", "P007", 1550 });

    dtProducts.Rows.Add(new object[] { "C001", "P004", 1550 });

    dtProducts.Rows.Add(new object[] { "C001", "P005", 150 });

    dtProducts.Rows.Add(new object[] { "C003", "P0011", 12550 });

    dtProducts.Rows.Add(new object[] { "C003", "P0012", 152350 });

    dtProducts.Rows.Add(new object[] { "C002", "P008", 15230 });

    dtProducts.Rows.Add(new object[] { "C002", "P009", 150 });

    dtProducts.Rows.Add(new object[] { "C003", "P0010", 150 });

    dtProducts.Rows.Add(new object[] { "C003", "P0013", 150 });

     

    objDataGridCollectionViewSource.Source = dtProducts.DefaultView;

    objDataGridCollectionViewSource.GroupDescriptions.Add(new DataGridGroupDescription("Category"));

    dataGridControl1.ItemsSource = objDataGridCollectionViewSource.View;

     

    }

    private void btnExpand_Click(object sender, RoutedEventArgs e)

    {

    objGroup.InitiallyExpanded = true;

    dataGridControl1.DefaultGroupConfiguration = objGroup;

    dataGridControl1.EndEdit();

    }

    private void btnCollapse_Click(object sender, RoutedEventArgs e)

    {

    objGroup.InitiallyExpanded = false;

    dataGridControl1.DefaultGroupConfiguration = objGroup;

    dataGridControl1.EndEdit();

    }

    }

    }

  •  09-01-2010, 5:14 PM Post no. 28554 in reply to 28543

    Re: Dynamic Expand/Collapse

    Hi Udaya,

    Changing the DefaultGroupConfigurations on the fly will not suddenly expand or collapse your groups. The InitiallyExpanded flag is used when the user creates a group, to know if the sub-groups should be initally expanded or collapsed.

    Also, if I remember correctly, the GroupConfigurations are only considered when the DataGrid is originally generated, meaning adding new ones after will not be taken into consideration.

    Please refer to the information found in the following forum threads instead:
       http://xceed.com/CS/forums/post/22536.aspx
       http://xceed.com/CS/forums/post/18937.aspx

     


    Diane Lafontaine
    Technical Support / Technical Support Team Lead
    Xceed Software Inc.
  •  09-02-2010, 1:39 AM Post no. 28560 in reply to 28554

    Re: Dynamic Expand/Collapse

    Thanks Diane,

            I applied the logic from the links given by you, it's working fine. here i given my updated code.

     

    using System;

    using System.Collections.Generic;

    using System.Linq;

    using System.Text;

    using System.Windows;

    using System.Windows.Controls;

    using System.Windows.Data;

    using System.Windows.Documents;

    using System.Windows.Input;

    using System.Windows.Media;

    using System.Windows.Media.Imaging;

    using System.Windows.Navigation;

    using System.Windows.Shapes;

    using Xceed.Wpf.DataGrid;

    using System.Data;

    namespace ExpandAndCollapse

    {

    /// <summary>

    /// Interaction logic for Window1.xaml

    /// </summary>

    public partial class Window1 : Window

    {

    DataTable dtProducts = new DataTable("Products");

    DataGridCollectionViewSource objDataGridCollectionViewSource = null;

    GroupConfiguration objGroup = new GroupConfiguration();

     

    public Window1()

    {

    InitializeComponent();

    objDataGridCollectionViewSource = new DataGridCollectionViewSource();

    dtProducts.Columns.Add("Category", typeof(string));

    dtProducts.Columns.Add("Product", typeof(string));

    dtProducts.Columns.Add("UnitPrice", typeof(double));

    dtProducts.Rows.Add(new object[] { "C001", "P001", 150 });

    dtProducts.Rows.Add(new object[] { "C001", "P002", 1350 });

    dtProducts.Rows.Add(new object[] { "C001", "P003", 12150 });

    dtProducts.Rows.Add(new object[] { "C002", "P006", 154230 });

    dtProducts.Rows.Add(new object[] { "C002", "P007", 1550 });

    dtProducts.Rows.Add(new object[] { "C001", "P004", 1550 });

    dtProducts.Rows.Add(new object[] { "C001", "P005", 150 });

    dtProducts.Rows.Add(new object[] { "C003", "P0011", 12550 });

    dtProducts.Rows.Add(new object[] { "C003", "P0012", 152350 });

    dtProducts.Rows.Add(new object[] { "C002", "P008", 15230 });

    dtProducts.Rows.Add(new object[] { "C002", "P009", 150 });

    dtProducts.Rows.Add(new object[] { "C003", "P0010", 150 });

    dtProducts.Rows.Add(new object[] { "C003", "P0013", 150 });

    objDataGridCollectionViewSource.Source = dtProducts.DefaultView;

    objDataGridCollectionViewSource.GroupDescriptions.Add(new DataGridGroupDescription("Category"));

    dataGridControl1.ItemsSource = objDataGridCollectionViewSource.View;

    }

    private void btnExpand_Click(object sender, RoutedEventArgs e)

    {

    foreach (CollectionViewGroup cvg in dataGridControl1.Items.Groups)

    {

    if (cvg != null)

    {

    dataGridControl1.ExpandGroup(cvg);

    }

    }

    }

    private void btnCollapse_Click(object sender, RoutedEventArgs e)

    {

    foreach (CollectionViewGroup cvg in dataGridControl1.Items.Groups)

    {

    if (cvg != null)

    {

    dataGridControl1.CollapseGroup(cvg);

    }

    }

    }

    }

    }

     

View as RSS news feed in XML
Contact | Site Map | Reviews | Legal Terms of Use | Trademarks | Privacy Statement Copyright 2011 Xceed Software Inc.