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
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 >>
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
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.
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 >>
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.
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
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
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 >>
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
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