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

Self-referencing Hierarchy

11 Answers 324 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Robert
Top achievements
Rank 1
Robert asked on 13 Oct 2009, 05:29 PM

Hi,

I recently read that self-referencing hierarchy is now supported by the radGridView.  Unfortunately, I have been unable to locate any demos, tutorials, or sample code to look at.  Is this information available?

Here is my current code that I'm working with:

private void SetupElementsGrid()  
{  
    radGvElements.Columns.Clear();  
    radGvElements.MasterGridViewTemplate.Columns.Add(new GridViewTextBoxColumn("ElementID"));  
    radGvElements.MasterGridViewTemplate.Columns.Add(new GridViewTextBoxColumn("ProtocolTemplateID"));  
    radGvElements.MasterGridViewTemplate.Columns.Add(new GridViewTextBoxColumn("ParentElementID"));  
    radGvElements.MasterGridViewTemplate.Columns.Add(new GridViewTextBoxColumn("ElementNumber"));  
    radGvElements.MasterGridViewTemplate.Columns.Add(new GridViewTextBoxColumn("ElementDesc"));  
 
    radGvElements.Columns["ElementID"].IsVisible = false;  
    radGvElements.Columns["ProtocolTemplateID"].IsVisible = false;  
    radGvElements.Columns["ParentElementID"].IsVisible = false;  
 
    radGvElements.Columns["ElementNumber"].HeaderText = "Element Number";  
    radGvElements.Columns["ElementDesc"].HeaderText = "Element Description";  
 
    radGvElements.MasterGridViewTemplate.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill;  
 
    GridViewTemplate childTemplate = new GridViewTemplate();     
    childTemplate.Columns.Add(new GridViewTextBoxColumn("ElementID"));  
    childTemplate.Columns.Add(new GridViewTextBoxColumn("ProtocolTemplateID"));  
    childTemplate.Columns.Add(new GridViewTextBoxColumn("ParentElementID"));  
    childTemplate.Columns.Add(new GridViewTextBoxColumn("ElementNumber"));  
    childTemplate.Columns.Add(new GridViewTextBoxColumn("ElementDesc"));  
 
    //childTemplate.Columns["ElementID"].IsVisible = false;  
    //childTemplate.Columns["ProtocolTemplateID"].IsVisible = false;  
    //childTemplate.Columns["ParentElementID"].IsVisible = false;  
 
    //childTemplate.Columns["ElementNumber"].HeaderText = "Element Number";  
    //childTemplate.Columns["ElementDesc"].HeaderText = "Element Description";  
 
    childTemplate.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill;  
      
    if(radGvElements.MasterGridViewTemplate.ChildGridViewTemplates.Count == 0)  
    {                     
        radGvElements.MasterGridViewTemplate.ChildGridViewTemplates.Add(childTemplate);  
    }  
      
    GridViewRelation relation = new GridViewRelation(radGvElements.MasterGridViewTemplate);  
    relation.ChildTemplate = childTemplate;  
    relation.RelationName = "ParentChild";  
    relation.ParentColumnNames.Add("ElementID");  
    relation.ChildColumnNames.Add("ParentElementID");  
    radGvElements.Relations.Add(relation);  
 
    ProtocolTemplate = (ProtocolTemplate)ProtocolTemplatesBindingSource.CurrencyManager.List[ProtocolTemplatesBindingSource.CurrencyManager.Position];  
    ElementsBindingSource.DataSource = ProtocolTemplate.Elements;  
    radGvElements.DataSource = ElementsBindingSource;  
                  
    radGvElements.DataSource = ElementsBindingSource;  
    childTemplate.DataSource = ElementsBindingSource;  

Sample data would look like this:

ElementID -- ProtocolTemplateID -- ParentElementID -- ElementNumber -- ElementDesc
1 -- 1 -- 1 -- 1.0 -- ParentLevel1
2 -- 1 -- 1 -- 1.1 -- ChildLevel1
3 -- 1 -- 1 -- 1.2 -- ChildLevel2
4 -- 1 -- 4 -- 2.0 -- ParentLevel2

With my current code, this displays as:

Row 1
-- Row1
-- Row2
-- Row3
Row 2
Row 3
Row 4
-- Row 4

It should be:

Row 1
-- Row 2
-- Row 3
Row 4

Also, another problem is with the portion of code I have commented out.  This code causes a NullReferenceException error to be thrown and I don't know why.

 

If anyone could help me out with the above code, or even point me in the direction of some reading material, I would be very grateful.

Thanks,

Robert

11 Answers, 1 is accepted

Sort by
0
David Kozikowski
Top achievements
Rank 1
answered on 14 Oct 2009, 03:38 PM

Yes I too am having a hard time making this work. I just bought the .net collection. I’m using the RADGridView. I want to be able to show a simple Invoice  - > InvoiceLine Item relationship

 

I would expect to be able to drop a RADGridView on a form set the data source to the dataset that has the relation I’m after. (A dataset that contains the 2 tables Invoice and InvoiceLineItem related in SQL by a Foreign Key constraint in the InvoiceLineItem  )  Set Auto Generate Hierarchy to true , hit F5 and see the form come up with data. (before.jpg)It all work this way but I done see the child Data nothing and when I hit one of the child tabs I get an out or range error. (after.jpg)

 The only data in the DB is 1 Invoice that has 2 line Line Items (sqltables.jpg)

Telerick Please provide a "how to" demo on this as it was a big selling point.

0
Julian Benkov
Telerik team
answered on 16 Oct 2009, 09:30 AM
Hello Robert and David,

Currently, RadGridView control does not support self-reference hierarchy, however this feature is in development and will be available very soon.

David, please verify you relations in the generated typed DataSet by Visual Studio. The generated hierarchy is strange in your scenario, it contains two InvoiceLineItem child templates. Please review the hierarchy section in our Demo application.

All the best,
Julian Benkov
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
David Kozikowski
Top achievements
Rank 1
answered on 16 Oct 2009, 10:03 AM
  • Hierarchy (Master-Detail Layout) with Tabbed Child View

    Presentation of related DataSets is a breeze with the supported hierarchical table structures, regardless of the grid mode (bound, unbound, or virtual). You can build self-referencing hierarchy from a single table in the datasource by specifying relations within the same level. Each hierarchical view comes with all editing features such as end-user sorting, filtering, aggregates, integrated cell editors, etc.

    RadGridView for WinForms from Telerik - Hierarchy Support

    In addition to the standard Hierarchy View, GridView for Windows Forms offers a Tabbed Child View which shows master-detail (one to many) relations in a tabbed layout while in hierarchy mode:
     
    RadGridView for WinForms - Detail Tabs

  • IT's says you can do self-reference hierarchy right is the doc. My relations are defined like the sqltables.jpg it's a simple.

    What I'm looking for is a demo example and I can't find one please be more precise on where to look Like WinForm sample 5 or some thing. I would also think this should be in the help docs called "Using  Hierarchy (Master-Detail Layout) with Tabbed Child View" or something. I just bought 5 licences because of this feature, now you saying that it's not implemented?

     

    0
    Julian Benkov
    Telerik team
    answered on 20 Oct 2009, 08:33 AM
    Hi David Kozikowski,

    In the current edition of RadGridView, you can build self-reference, but you have to add child templates manually and bind to the same data source many times.

    Full featured self-reference is the automatic generation of hierarchy from the desired data source if a property like IsSelfReference is set for the extended Relation object. We currently work on this new functionality and it will be available very soon but after Q3 2009.

    Sincerely yours,
    Julian Benkov
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
    0
    Robert
    Top achievements
    Rank 1
    answered on 20 Oct 2009, 02:16 PM
    Hi Julian,

    "In the current edition of RadGridView, you can build self-reference, but you have to add child templates manually and bind to the same data source many times."

    As you can see from my original sample code, I am building and adding the child templates manually and have bound to the same data source twice, yet this still does not work.

    Am I doing something wrong?  Can you please review my code and see what I'm missing?  Any assistance would be greatly appreciated...

    Thanks,

    Robert
    0
    Julian Benkov
    Telerik team
    answered on 23 Oct 2009, 08:50 AM
    Hi Robert,

    Sorry for the introduced inconvenience.

    In order to make self-reference scenario work in this situation, you must also filter the data by its parent/child key. The data source for MasterGridVewTemplate must contain all rows filtered by 'ParentId == null'. For this child template, rows must be filtered by 'ParentId != null". In general, this introduce more lines of code and complexity. To avoid this and make the process simpler, we currently implement a new mechanism where you will only need to set the DataSource of RadGridView and add just one self-reference relation.

    All the best,
    Julian Benkov
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
    0
    Paul
    Top achievements
    Rank 1
    answered on 25 Apr 2014, 08:29 PM
    Are there any examples on the Telerik site on how to filter a self-reference grid?

    Thanks

    Paul
    0
    Dess | Tech Support Engineer, Principal
    Telerik team
    answered on 30 Apr 2014, 08:07 AM
    Hello Paul,

    Thank you for writing.

    In our Demo Application > GridView > Filtering section there are several examples related to data filtering in grid. You can find an example about self-reference in  Demo Application >> GridView >> Hierarchy section. Both of the examples can be combined to enable filtering in self-reference hierarchy:
    public Form1()
    {
        InitializeComponent();
     
        this.radGridView1.Columns.Add("Id");
        this.radGridView1.Columns.Add("Name");
        this.radGridView1.Columns.Add("ParentFolderId");
        this.radGridView1.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill;
        this.radGridView1.EnableFiltering = true;
     
        this.radGridView1.Relations.AddSelfReference(this.radGridView1.MasterTemplate, "Id", "ParentFolderId");
     
        for (int i = 1; i <= 7; i++)
        {
            radGridView1.Rows.Add(i, "Parent" + i, 0);
            for (int j = 8; j <= 15; j++)
            {
                dynamic childIndex = ((i - 1) * 9 + j + i);
                radGridView1.Rows.Add(childIndex, "Child" + childIndex, i);
            }
        }
    }

    Please have a look at the attached gif file, illustrating better the available functionality. 

    I hope this information helps. Should you have further questions, I would be glad to help.

    Regards,
    Desislava
    Telerik
     
    Check out Telerik Analytics, the service which allows developers to discover app usage patterns, analyze user data, log exceptions, solve problems and profile application performance at run time. Watch the videos and start improving your app based on facts, not hunches.
     
    0
    Fariba
    Top achievements
    Rank 1
    answered on 20 Apr 2016, 06:23 AM

    Hi,
    Dear Dess,

    I am using Self-referencing Hierarchy radgridview and I want to have customize sample search on it
    I have text-box on the form and I want to find entered  text in both parents and children ,the search starts from the selected row to down (Like search in browser's page)

    Is there any similar sample which shows me how can I implement this?I search a lot but I could not find any suitable sample.

     

    Thanks

    Fariba

    0
    Fariba
    Top achievements
    Rank 1
    answered on 20 Apr 2016, 06:24 AM

    I just find below code which only search in one level

        private void radTextBox1_TextChanged(object sender, EventArgs e)
            {
                if (radGridView1.SelectedRows != null && radGridView1.SelectedRows.Count > 0 && radTextBox1.Text.Trim() != string.Empty)
                {
                    GridViewHierarchyRowInfo hierarchyRow = radGridView1.SelectedRows[0] as GridViewHierarchyRowInfo;
                    if (hierarchyRow != null)
                    {
                        IterateChildRows(hierarchyRow);
                    }

                }
            }

     

     

            private void IterateChildRows(GridViewHierarchyRowInfo rowInfo)
            {
                GridViewInfo currentView = rowInfo.ActiveView;
                foreach (GridViewInfo view in rowInfo.Views)
                {
                    rowInfo.ActiveView = view;
                    foreach (GridViewRowInfo row in rowInfo.ChildRows)
                    {
                        if (row.Cells["name"].Value.ToString().Contains(radTextBox1.Text.Trim()))
                        {
                            radGridView1.CurrentRow = row;
                            return;
                        }
                        GridViewHierarchyRowInfo hierarchyRow = row as GridViewHierarchyRowInfo;
                        if (hierarchyRow != null)
                        {
                            IterateChildRows(hierarchyRow);
                        }
                    }
                }
                rowInfo.ActiveView = currentView;
            }

    0
    Dess | Tech Support Engineer, Principal
    Telerik team
    answered on 20 Apr 2016, 12:39 PM
    Hello Fariba,

    Thank you for writing.

    Please refer to our Demo application >> GridView >> Filtering >> Custom Filtering example which is quite useful on this topic. It uses the custom filtering functionality that RadGridView offers to control the filtering logic.

    I hope this information helps. Should you have further questions I would be glad to help.

     Regards,
    Dess
    Telerik
    Do you need help with upgrading your AJAX, WPF or WinForms project? Check the Telerik API Analyzer and share your thoughts.
    Tags
    GridView
    Asked by
    Robert
    Top achievements
    Rank 1
    Answers by
    David Kozikowski
    Top achievements
    Rank 1
    Julian Benkov
    Telerik team
    Robert
    Top achievements
    Rank 1
    Paul
    Top achievements
    Rank 1
    Dess | Tech Support Engineer, Principal
    Telerik team
    Fariba
    Top achievements
    Rank 1
    Share this question
    or