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

Disable Relation

10 Answers 170 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Michel
Top achievements
Rank 1
Michel asked on 23 Aug 2011, 09:39 PM
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

10 Answers, 1 is accepted

Sort by
0
Julian Benkov
Telerik team
answered on 26 Aug 2011, 07:57 AM
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 >>

0
Michel
Top achievements
Rank 1
answered on 26 Aug 2011, 09:28 AM
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
0
Michel
Top achievements
Rank 1
answered on 30 Aug 2011, 06:19 AM
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.
0
Julian Benkov
Telerik team
answered on 31 Aug 2011, 08:44 AM
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 >>

0
Michel
Top achievements
Rank 1
answered on 14 Sep 2011, 10:43 PM
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.
0
Accepted
Emanuel Varga
Top achievements
Rank 1
answered on 15 Sep 2011, 08:28 AM
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
0
Ed
Top achievements
Rank 1
answered on 16 Sep 2011, 07:40 PM
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
0
Julian Benkov
Telerik team
answered on 20 Sep 2011, 08:47 AM
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 >>

0
Michel
Top achievements
Rank 1
answered on 20 Sep 2011, 09:09 PM
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

0
Ed
Top achievements
Rank 1
answered on 21 Sep 2011, 02:03 PM
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
Tags
GridView
Asked by
Michel
Top achievements
Rank 1
Answers by
Julian Benkov
Telerik team
Michel
Top achievements
Rank 1
Emanuel Varga
Top achievements
Rank 1
Ed
Top achievements
Rank 1
Share this question
or