Disable Relation

11 posts, 1 answers
  1. Michel
    Michel avatar
    5 posts
    Member since:
    Jun 2012

    Posted 23 Aug 2011 Link to this post

    Hi,

    I am using a RadGridView with a selfreference relation to display items from a DataTable. This works great but my customers want to be able to toggle between hierarchical view and flat view of the data.
    I try to delete the relation without any effect.

    Is there a way to do such thing ?

    Regards
  2. Julian Benkov
    Admin
    Julian Benkov avatar
    1135 posts

    Posted 26 Aug 2011 Link to this post

    Hello Michel,

    To support this functionality you must remove the child GridViewTemplare when switching to flat mode and add the same GridViewTemplate back when switching to hierarchy mode:
    GridViewTemplate childTemplate = this.radGridView1.Templates[0];
    this.radGridView1.Templates.RemoveAt(0);

    this.radGridView1.Templates.Add(childTemplate);

    I hope this information is useful. Let me know if you need further assistance.

    Regards,
    Julian Benkov
    the Telerik team

    Thank you for being the most amazing .NET community! Your unfailing support is what helps us charge forward! We'd appreciate your vote for Telerik in this year's DevProConnections Awards. We are competing in mind-blowing 20 categories and every vote counts! VOTE for Telerik NOW >>

  3. UI for WinForms is Visual Studio 2017 Ready
  4. Michel
    Michel avatar
    5 posts
    Member since:
    Jun 2012

    Posted 26 Aug 2011 Link to this post

    Hi Julian,

    Thanks for your answer.

    I am not able to test your solution yet  but as I use a self relation, child and parent templates are the same (MasterTemplate). Then will your solution work ?

    Thanks
  5. Michel
    Michel avatar
    5 posts
    Member since:
    Jun 2012

    Posted 30 Aug 2011 Link to this post

    Hi Julian,

    I try suggested solution but it does not change anything.
    For your information radGridView1.Templates.Count is equal to 0. Maybe because, it is a self-reference hierarchy.

    So please suggest.

    Thanks.
  6. Julian Benkov
    Admin
    Julian Benkov avatar
    1135 posts

    Posted 31 Aug 2011 Link to this post

    Hello Michel,

    The previous solution cannot be used for self-reference hierarchy.  In this situation you can try to assign the self-reference Relation to an external variable and add/remove it from RadGridView Relations collection to switch on/off self-reference hierarchy mode:

    GridViewRelation selfReferenceRelation = this.radGridView1.Relations[0];
     this.radGridView1.Relations.Remove(selfReferenceRelation);
     
    ...
     
     this.radGridView1.Relations.Add(selfReferenceRelation);

    Best wishes,
    Julian Benkov
    the Telerik team

    Thank you for being the most amazing .NET community! Your unfailing support is what helps us charge forward! We'd appreciate your vote for Telerik in this year's DevProConnections Awards. We are competing in mind-blowing 20 categories and every vote counts! VOTE for Telerik NOW >>

  7. Michel
    Michel avatar
    5 posts
    Member since:
    Jun 2012

    Posted 14 Sep 2011 Link to this post

    Thanks for your answer and sorry for the late,

    I tried your suggested solution but it has no effect.

    In fact here is my code :
    If RadGridView1.Relations.LongCount() <> 0 Then
        relation = RadGridView1.Relations(0)
        RadGridView1.Relations.Remove(relation)
    Else
        RadGridView1.Relations.Add(relation)
    End If

    After executing it, if I do a sort, display is partially updated (view switches between flat/hierarchical view), but scrollbar is not updated, "+" is not displayed when I go back to hierarchy, "+" is still displayed when I go to flat mode.
     
    So please suggest.
  8. Answer
    Emanuel Varga
    Emanuel Varga avatar
    1336 posts
    Member since:
    May 2010

    Posted 15 Sep 2011 Link to this post

    Hello guys,

    Actually to remove a self referencing relation you have to do more than that...
    In my point of view, for this to work, you should do the following:
    private void SwithMode(bool createRelation)
    {
        var dataSource = radGridView1.DataSource;
        radGridView1.Relations.Clear();
        radGridView1.MasterTemplate.HierarchyDataProvider = null;
        radGridView1.DataSource = null;
        //recreate relation here
        if (createRelation)
            radGridView1.Relations.AddSelfReference(radGridView1.MasterTemplate, "Id", "ParentId");
     
        //reset the datasource
        radGridView1.DataSource = dataSource; // throws null reference...
    }

    But like i said there I'm either missing something or there is a bug, because it is throwing a null reference exception with this stack trace:
    at Telerik.WinControls.UI.GridViewSelfReferenceDataProvider.Refresh()
    at Telerik.WinControls.Data.NotifyCollectionChangedEventHandler.Invoke(Object sender, NotifyCollectionChangedEventArgs e)
    at Telerik.WinControls.Data.RadListSource`1.OnCollectionChanged(NotifyCollectionChangedEventArgs e)
    at Telerik.WinControls.Data.RadListSource`1.EndUpdate(Boolean notifyUpdates)
    at Telerik.WinControls.Data.RadListSource`1.EndUpdate()
    at Telerik.WinControls.Data.RadListSource`1.Initialize()
    at Telerik.WinControls.Data.RadListSource`1.Bind(Object dataSource, String dataMember)
    at Telerik.WinControls.Data.RadListSource`1.set_DataSource(Object value)
    at Telerik.WinControls.UI.GridViewTemplate.set_DataSource(Object value)
    .....

    Best Regards,
    Emanuel Varga
  9. Ed
    Ed avatar
    8 posts
    Member since:
    Apr 2010

    Posted 16 Sep 2011 Link to this post

    Emanuel,
    Have you had any luck with this?  I get a very similiar error trying to do the same thing.  So far I haven't been able to remove the self reference.
    Thanks,
    Ed
  10. Julian Benkov
    Admin
    Julian Benkov avatar
    1135 posts

    Posted 20 Sep 2011 Link to this post

    Hello Ed,

    You can try to Reset the current self-reference hierarchy using this solution:
    private void SwithMode(bool createRelation)
    {
        var dataSource = radGridView1.DataSource;
        radGridView1.MasterTemplate.Reset();
        radGridView1.DataSource = dataSource;
         
        //recreate relation here
        if (createRelation)
        {
            radGridView1.Relations.AddSelfReference(radGridView1.MasterTemplate, "Id", "ParentId");
        }
    }

    I hope this helps.

    All the best,
    Julian Benkov
    the Telerik team

    Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

  11. Michel
    Michel avatar
    5 posts
    Member since:
    Jun 2012

    Posted 20 Sep 2011 Link to this post

    Hi,

    I found following solution (vb.net):
    Dim dataSource As Object = RadGridView1.DataSource
    Dim dataMember As String = RadGridView1.DataMember
     
    If RadGridView1.Relations.LongCount() <> 0 Then
        relation = RadGridView1.Relations(0)
        RadGridView1.Relations.Remove(relation)
        RadGridView1.MasterTemplate.Reset()
     
        RadGridView1.DataSource = dataSource
        RadGridView1.DataMember = dataMember
    Else
        RadGridView1.MasterTemplate.Reset()
     
        RadGridView1.DataSource = dataSource
        RadGridView1.DataMember = dataMember
     
        RadGridView1.Relations.Add(relation)
    End If

  12. Ed
    Ed avatar
    8 posts
    Member since:
    Apr 2010

    Posted 21 Sep 2011 Link to this post

    Thanks for the suggestion - I tried it quickly, but realized that calling MasterTemplate.Reset() caused my columns to be lost.  I'm not able to deal with that for right now (try again another time).

    Here is the workaround that I had already applied (I'm doing this to allow filtering to work properly).  Basically, my relation is built on the ParentId column and if I remove the column from the grid it switches back to a flat list.
    if(0 == m_grid.FilterDescriptors.Count)
    {
        if(!m_grid.Columns.Contains("ParentId"))
        {
            GridViewDataColumn col = new GridViewTextBoxColumn("ParentId", "ParentId");
            col.VisibleInColumnChooser = true;
            col.IsVisible = false;
     
            m_grid.Columns.Add(col);
        }
     
        m_grid.Relations.AddSelfReference(m_grid.MasterTemplate, "Id", "ParentId");
    }
    else
    {
        m_grid.Relations.Clear();
        m_grid.Columns.Remove("ParentId");
    }


    Thanks again!
    Ed
Back to Top
UI for WinForms is Visual Studio 2017 Ready