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

Custom Hierarchy: Adding ChildTableDefinitions to the expandable row creates exception

8 Answers 244 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Jitin Vishwakarma
Top achievements
Rank 1
Jitin Vishwakarma asked on 13 Jan 2010, 10:04 PM
Hi,

I had a piece of code to add hierarchy to the GridView that works fine in version Q2 2009. I add Child grid at runtime where I cannot even use the binding because the structure of the data is not defined and is based on user interactions.
I had a code that looked something like this:

void TableDefinition_PreviewDataRecordCreate(object sender, DataRecordCreateEventArgs e)
{
GridViewTableDefinition detailDefinition = new GridViewTableDefinition();
detailDefinition.AutoGenerateFieldDescriptors = false;

// create colMultiplier
detailDefinition.FieldDescriptors.Add(colMultiplier);

// Create Data Source for Child Nodes
var sourceDataRecs = new List<object>();
// fill sourceDataRecs

detailDefinition.DataSource = sourceDataRecs;
e.IsExpandableRecord = true;
e.ChildTableDefinitions.Add(detailDefinition);
e.GridViewDataControl.ChildTableDefinitions.Add(detailDefinition);
}

This is replaced by the following code in Q3:
    private void radTargetGridNodes_RowLoaded(object sender, RowLoadedEventArgs e)
    {
        GridViewTableDefinition detailDefinition = new GridViewTableDefinition();
        detailDefinition.AutoGenerateFieldDescriptors = false;
        detailDefinition.FieldDescriptors.Add(colMultiplier);

        // Create Data Source for Child Nodes
        var sourceDataRecs = new List<object>();

        detailDefinition.DataSource = sourceDataRecs;        
        ((GridViewRow)e.Row).IsExpandable = true;
        e.GridViewDataControl.ChildTableDefinitions.Add(detailDefinition);  ----> THIS LINE CREATES EXCEPTION
        /// Exception - HRESULT E_FAIL  Managed RunTime Error #4004
    }

Would there be a better way to do this?

Thanks,
Jitin

8 Answers, 1 is accepted

Sort by
0
Vlad
Telerik team
answered on 14 Jan 2010, 07:11 AM
Hi,

Since PreviewDataRecordCreate you can use a bit different approach to achieve custom hierarchy demonstrate here:
http://demos.telerik.com/silverlight/#GridView/Hierarchy/CustomHierarchy

Regards,
Vlad
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
Jitin Vishwakarma
Top achievements
Rank 1
answered on 14 Jan 2010, 03:34 PM
Hi Vlad,

If you look closely in the code provided above, I am using RowLoaded event for Q3 instead of PreviewDataRecordCreate.
I have seen the Demo/examples code for Q3. But the whole point for my question is that I cannot bind the child rows until during runtime. That is the reason why I had to add GridViewTableDefinition instance to ChildTableDefinitions of the parent row. Why does this no longer work in Q3? Or am I doing something wrong?

Thanks,
Jitin
0
Vlad
Telerik team
answered on 14 Jan 2010, 03:37 PM
Hi Jitin,

Sorry, I've missed this!

Still you do not need to add child tables dynamically - please check the demo I've posted in my previous reply.

Kind regards,
Vlad
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
Frank Gusche
Top achievements
Rank 1
answered on 14 Jan 2010, 04:28 PM
Hi,

I have exactly the same problem. It's not possible to add a GridViewTableDefinition in code behind.
You always get the error "HRESULT E_FAIL ...".
Sorry Vlad, the demo isn't helpful.

Any other ideas?

Thanks,
Frank
0
Vlad
Telerik team
answered on 15 Jan 2010, 06:51 AM
Hello Frank,

Can you post more info why you need to add child tables dynamically? Our demo demonstrate how to create custom hierarchy - why the demo is not helpful? Please clarify.

Best wishes,
Vlad
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
Frank Gusche
Top achievements
Rank 1
answered on 19 Jan 2010, 09:05 AM
Hi Vlad,

my colleague had solved the problem.
In the new version you have to define a "ChildTableDefinition" in the foreground-code.



<telerikGridView:RadGridView       
                        Grid.Row="1"                
                        Margin="10"  
                        ItemsSource="{Binding Holdings}"  
                        x:Name="_gridHolding" 
                        ColumnsWidthMode="None"  
                        ShowGroupPanel="False"                         
                        IsReadOnly="True"  
                        ScrollMode="RealTime" 
                        AutoGenerateColumns="True"
 
                            <telerikGridView:RadGridView.ChildTableDefinitions> 
                                <telerikGridView:GridViewTableDefinition> 
 
                                </telerikGridView:GridViewTableDefinition> 
                            </telerikGridView:RadGridView.ChildTableDefinitions> 
                            <telerikGridView:RadGridView.HierarchyChildTemplate> 
                                <DataTemplate> 
                                    <StackPanel> 
                                        <telerikGridView:RadGridView  
                                                AutoGenerateColumns="True" 
                                                x:Name="_gridTransactions" 
                                                ShowGroupPanel="False" 
                                                IsReadOnly="True"  
                                                Loaded="_gridTransactions_Loaded" 
                                                > 
                                        </telerikGridView:RadGridView> 
                                    </StackPanel> 
                                </DataTemplate> 
                            </telerikGridView:RadGridView.HierarchyChildTemplate> 
 
                        </telerikGridView:RadGridView> 


At the outer grid you can now define a "RowLoaded-Event":

void _gridHolding_RowLoaded(object sender, Telerik.Windows.Controls.GridView.RowLoadedEventArgs e) 
        { 
            GridViewRow row = e.Row as GridViewRow; 
            InstrumentsHoldings holding = e.DataElement as InstrumentsHoldings; 
 
            if(row != null && holding != null
            { 
                row.IsExpandable = true
                 
                GridViewTableDefinition detailDefinition = new GridViewTableDefinition(); 
... 
 


In the inner grid you have to define a "Loaded-Event" where you bind your data.

private void _gridTransactions_Loaded(object sender, RoutedEventArgs e) 
        {            
            var k = sender as GridViewDataControl; 
 
            var cont = k.ParentRow.DataContext; 
           
            k.ItemsSource = ItemsSource; 
        } 


When you haven't the "ChildTableDefinitions" in the foreground-code and want do add some from the code behind then the error "HRESULT E_FAIL ..." occurs.


That worked for me!

Hope it helps.


Regards,
Frank


0
John
Top achievements
Rank 1
answered on 14 Apr 2010, 04:45 PM
The demo link does not seem to be working.  I keep getting redirected to install Silverlight, which is definitely installed on my system. 

I have the same problem, but really I don't know how to do this any other way.  We have an XML column in the parent data that we display dynamically as a child table.  The XML can vary from row to row, so I create the child table on the fly.  I can't seem to get this to work on the row loaded event.  Here are a few snippets:
      private void DetailsGrid_RowLoaded(object sender, RowLoadedEventArgs e)  
        {  
 
            GridViewRow row = e.Row as GridViewRow;  
 
            if (row != null)  
            {  
 
                if (row.DataContext is DDO_SimpleXMLBaseClass)  
                {  
                    DDO_XMLBaseClassData data = (DDO_XMLBaseClassData)row.DataContext;  
                    if (data.TransactionXml.Count() > 0)  
                    {  
                        row.IsExpandable = true;  
                        e.GridViewDataControl.ChildTableDefinitions.Add(getTableDefFromXML(data.TransactionXml[0].ToString()));  
                    } 
...
       private GridViewTableDefinition getTableDefFromXML(string xml)  
        {  
            GridViewTableDefinition detailDefinition = new GridViewTableDefinition();  
            XmlDataProvider xdp = new XmlDataProvider();  
            xdp.IsAsynchronous = false;  
            System.Xml.XmlDocument doc = new System.Xml.XmlDocument();  
            doc.LoadXml(xml);  
            xdp.Document = doc;  
            xdpxdp.XPath = xdp.Document.FirstChild.Value;  
            xdp.XPath = "/" + doc.DocumentElement.Name;  
            detailDefinition.DataSource = new ObservableCollection<XmlNode>((IEnumerable<XmlNode>)xdp.Data);  
            return detailDefinition;  
        } 

Not sure what to try at this point.
Thanks

John
0
Vlad
Telerik team
answered on 16 Apr 2010, 07:10 AM
Hello,

Can you post more info about your Silverlight version? I've just tried the demo using both Silverlight 3 and Silverlight4 and everything worked as expected.

Kind regards,
Vlad
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.
Tags
GridView
Asked by
Jitin Vishwakarma
Top achievements
Rank 1
Answers by
Vlad
Telerik team
Jitin Vishwakarma
Top achievements
Rank 1
Frank Gusche
Top achievements
Rank 1
John
Top achievements
Rank 1
Share this question
or