Rows are repeating in Self Hiererachy

2 posts, 0 answers
  1. Narendra
    Narendra avatar
    25 posts
    Member since:
    Jul 2010

    Posted 13 Jul 2010 Link to this post

    Hi,

    I'm working radGridView control for displaying self referencial and hierarchal data, i'm using the latest telerik binaries (i.e.WPF Q1 2010 SP2).

    With my current code, this displays as:

    Task 1
    -- Task101
    -- Task102
    Task 2
    -- Task201
    -- Task202
    Task101
    Task102
    Task201
    Task202

    I dont want to display Task101,Task102,Task201,Task202 in the main hirerarchy list, as those are the childs to Task1 and Task2

    Code Pasted Below:

    XAML:

     <Grid>
            <telerik:RadGridView x:Name="RadGridView1"
                 DataLoading="RadGridView1_DataLoading" RowLoaded="RadGridView1_RowLoaded" GridLinesVisibility="Horizontal"
                CanUserFreezeColumns="False" IsReadOnly="True" AutoGenerateColumns="False"  >

                <telerik:RadGridView.Columns>
                    <telerik:GridViewDataColumn  DataMemberBinding="{Binding TaskId}"  />
                   
                    <telerik:GridViewDataColumn Header="Task Name" DataMemberBinding="{Binding TaskName}" />

                    <telerik:GridViewDataColumn Header="Read">
                        <telerik:GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <CheckBox x:Name="checkBoxRead" IsChecked="{Binding Read}" />
                            </DataTemplate>
                        </telerik:GridViewColumn.CellTemplate>
                    </telerik:GridViewDataColumn>

                    <telerik:GridViewDataColumn Header="Write">
                        <telerik:GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <CheckBox x:Name="checkBoxWrite" IsChecked="{Binding Write}" />
                            </DataTemplate>
                        </telerik:GridViewColumn.CellTemplate>
                    </telerik:GridViewDataColumn>

                </telerik:RadGridView.Columns>

                <telerik:RadGridView.ChildTableDefinitions>
                    <telerik:GridViewTableDefinition>
                        <telerik:GridViewTableDefinition.Relation  >
                            <telerik:TableRelation IsSelfReference="True" >
                                <telerik:TableRelation.FieldNames>
                                    <telerik:FieldDescriptorNamePair
                                        ParentFieldDescriptorName="TaskId"
                                        ChildFieldDescriptorName="ParentTaskId" />
                                </telerik:TableRelation.FieldNames>
                            </telerik:TableRelation>
                        </telerik:GridViewTableDefinition.Relation>
                    </telerik:GridViewTableDefinition>
                </telerik:RadGridView.ChildTableDefinitions>

            </telerik:RadGridView>
        </Grid>

     

     

     

     

     

     

     

     

     

    .CS Code:

    /// <summary>
        /// Interaction logic for MainWindow.xaml
        /// </summary>
        public partial class MainWindow : Window
        {
            public MainWindow()
            {
                InitializeComponent();
                //RadGridView1. .MasterTableView.FilterExpression = "ParentID = 0";
                RadGridView1.ItemsSource = GetTasks();
               
            }

     

            private List<Task> GetTasks()
            {
                List<Task> tasks = new List<Task>();

                tasks.Add(new Task { TaskId = 1, TaskName = "Task1", Read = true, Write = true, ParentTaskId = 0 });
                tasks.Add(new Task { TaskId = 2, TaskName = "Task2", Read = false, Write = true, ParentTaskId = 0 });

                tasks.Add(new Task { TaskId = 101, TaskName = "Task101", Read = true, Write = true, ParentTaskId = 1 });
                tasks.Add(new Task { TaskId = 102, TaskName = "Task102", Read = false, Write = false, ParentTaskId = 1 });

                tasks.Add(new Task { TaskId = 201, TaskName = "Task201", Read = true, Write = true, ParentTaskId = 2 });
                tasks.Add(new Task { TaskId = 202, TaskName = "Task202", Read = false, Write = false, ParentTaskId = 2 });

                //tasks.Add(new Task { TaskId = 1011, TaskName = "Task1011", Read = true, Write = false, ParentTaskId = 101 });

             
                return tasks;
            }

            private bool HasSubTasks(Task currentTask)
            {
                return
                (from task in (IEnumerable<Task>)this.RadGridView1.ItemsSource
                 where task.ParentTaskId == currentTask.TaskId
                 select task).Any();
            }

            private void RadGridView1_RowLoaded(object sender, Telerik.Windows.Controls.GridView.RowLoadedEventArgs e)
            {

                GridViewRow row = e.Row as GridViewRow;
                Task currentTask = e.DataElement as Task;

                if (row != null && currentTask != null)
                {
                    row.IsExpandable = this.HasSubTasks(currentTask);
                }
            }

            private void RadGridView1_DataLoading(object sender, GridViewDataLoadingEventArgs e)
            {
                GridViewDataControl dataControl = (GridViewDataControl)sender;

                if (dataControl.ParentRow != null)
                {
                    dataControl.ShowGroupPanel = false;
                    dataControl.AutoGenerateColumns = false;
                    dataControl.CanUserFreezeColumns = false;
                    dataControl.IsReadOnly = false;
                    dataControl.ChildTableDefinitions.Clear();

                    GridViewDataColumn column = new GridViewDataColumn();
                    column.DataMemberBinding = new Binding("TaskId");
                    dataControl.Columns.Add(column);

                    column = new GridViewDataColumn();
                    column.DataMemberBinding = new Binding("TaskName");
                    dataControl.Columns.Add(column);

                    column = new GridViewDataColumn();
                    column.DataMemberBinding = new Binding("Read");
                    dataControl.Columns.Add(column);

                    column = new GridViewDataColumn();
                    column.DataMemberBinding = new Binding("Write");
                    dataControl.Columns.Add(column);
                }

            }
        }

        public class Task
        {
            public int TaskId { get; set; }
            public string TaskName { get; set; }
            public bool Read { get; set; }
            public bool Write { get; set; }

            public int ParentTaskId { get; set; }

        }

     

    Thanks,
    -Narendra

  2. Veselin Vasilev
    Admin
    Veselin Vasilev avatar
    2992 posts

    Posted 14 Jul 2010 Link to this post

    Hello Narendra,

    To hide that rows please subscribe to the RowLoaded event of the grid and check whether the data should be shown or not.

    Here is an example:

    private void RadGridView_RowLoaded(object sender, RowLoadedEventArgs e)
    {
        Employee emp = e.DataElement as Employee;
        GridViewRow row = e.Row as GridViewRow;
     
        if (row != null && emp != null)
        {
            if (emp.ReportsTo.HasValue)
            {
                row.Visibility = Visibility.Collapsed;
            }
        }
    }

    Hope this helps.

    All the best,
    Veskoni
    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
  3. UI for WinForms is Visual Studio 2017 Ready
Back to Top