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

[Solved] Gridview Hierarchy VB

4 Answers 143 Views
GridView
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Martin Roussel
Top achievements
Rank 1
Martin Roussel asked on 09 Feb 2010, 09:27 PM
Hi everyone,

im trying to create a 2-level hierarchy Gridview in my application and for some reason, Im struggling to create the child table structure (and inserting data) into my parent grid. Im obtaining my parent grid with expendable rows successfully . I try to replicate what the Hierarchy sample from this site shows but im always getting datacontrol.ParentRow = nothing in my DataLoading event. Here is parts of my code:

xmlns:telerikGridView="clr-namespace:Telerik.Windows.Controls;assembly=Telerik.Windows.Controls.GridView" 
xmlns:telerikData="clr-namespace:Telerik.Windows.Data;assembly=Telerik.Windows.Data" 
 
<telerikGridView:RadGridView x:Name="grdData" d:LayoutOverrides="GridBox"
                <telerikGridView:RadGridView.ChildTableDefinitions> 
                    <telerikGridView:GridViewTableDefinition> 
                        <telerikGridView:GridViewTableDefinition.Relation> 
                            <telerikData:PropertyRelation ParentPropertyName="Grid_Details"/> 
                        </telerikGridView:GridViewTableDefinition.Relation> 
                    </telerikGridView:GridViewTableDefinition> 
                </telerikGridView:RadGridView.ChildTableDefinitions> 
                    </telerikGridView:RadGridView> 

Private mData As ObservableCollection(Of GridUDData)

Private
 Sub Grid_DataLoading(ByVal sender As ObjectByVal e As Telerik.Windows.Controls.GridView.GridViewDataLoadingEventArgs) Handles grdData.DataLoading 
 
 
        Dim dataControl As GridViewDataControl = TryCast(sender, GridViewDataControl) 
 
        If Not (dataControl.ParentRow Is NothingThen     'IT NEVER ENTERS IN HERE!
 
            dataControl.CanUserFreezeColumns = False 
            dataControl.ShowGroupPanel = False 
            dataControl.AutoGenerateColumns = False 
 
            Dim column As GridViewDataColumn = New GridViewDataColumn() 
            column.Header = "Column1" 
            dataControl.Columns.Add(column)

            dataControl.ItemsSource = mData
 
 
        End If 
 
 
    End Sub 






As you can see, im using VB and all examples ive found are in CS so maybe it's just a wrong convertion.

My second question is can someone please show me how to insert data into this child grid? Can it be done in the RowLoaded event?

Best regards


4 Answers, 1 is accepted

Sort by
0
Milan
Telerik team
answered on 10 Feb 2010, 07:24 AM
Hello Martin Roussel,

If you are building a property hierarchy you could simply use HierarchyChild template to setup your child grid in XAML only. Our Hierarchy Child Template example demonstrates this approach.

Basically you just need a dummy TableDefinition:

<telerik:RadGridView x:Name="RadGridView1" CanUserFreezeColumns="False" ItemsSource="{Binding Orders}"  IsReadOnly="True" AutoGenerateColumns="False">
            <telerik:RadGridView.ChildTableDefinitions>
                <telerik:GridViewTableDefinition />
            </telerik:RadGridView.ChildTableDefinitions>

After that define the child template:

<telerik:RadGridView.HierarchyChildTemplate>
               <DataTemplate>
                   <telerik:RadGridView x:Name="RadGridView1" CanUserFreezeColumns="False" AutoGenerateColumns="False" ItemsSource="{Binding Grid_Details}"  ShowGroupPanel="False" IsReadOnly="True">
                       <telerik:RadGridView.Columns>
                           <telerik:GridViewDataColumn DataMemberBinding="{Binding ProductID}" Header="Product ID" />
                           <telerik:GridViewDataColumn DataMemberBinding="{Binding UnitPrice}" DataFormatString="{}{0:c}" Header="Unit Price" />
                           <telerik:GridViewDataColumn DataMemberBinding="{Binding Quantity}" Header="Quantity" />
                           <telerik:GridViewDataColumn DataMemberBinding="{Binding Discount}" Header="Discount" />
                       </telerik:RadGridView.Columns>
                   </telerik:RadGridView>
               </DataTemplate>
           </telerik:RadGridView.HierarchyChildTemplate>

You should be able to insert data into the child grid by pressing the Insert key when a child grid is focused. Could you please take a look at our help topics regarding data management.

Best wishes,
Milan
the Telerik team

Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
Follow the status of features or bugs in PITS and vote for them to affect their priority.
0
Martin Roussel
Top achievements
Rank 1
answered on 10 Feb 2010, 12:39 PM
Thanks for the reply,

First, Am I right saying that defining data binding and columns into the XAML, im stuck using the same grid structure everytime for the same grid control? I prefer doing my data binding into my VB since my grid needs to be dynamic (multiple table structures can be inserted into the same grid control at runtime...based on some conditions). In other words, can we put binding and column definition conditions into XAML?

Second, since im new to XAML, im not sure how the binding works and would like a complete example of it. In your reply, where and how "Orders" is defined?

Third, by "inserting data", i meant from my code and not the user...is the "dataControl.ItemsSource = mData" i have in my code block right?

And Finally, is there something im doing wrong into my DataLoading event?

Thanks again




0
Martin Roussel
Top achievements
Rank 1
answered on 11 Feb 2010, 12:34 PM
Milan, can you please show me how  and where your Orders and Grid_Details classes are defined? Do they need inheritance in order for the binding works?

Also, I still dont get why the CS line "if (dataControl.ParentRow != null)" that I converted to "If Not (dataControl.ParentRow Is NothingThen" doesnt seem to be right...since I can never get in this If statement.


Anyone can help please?
0
Milan
Telerik team
answered on 15 Feb 2010, 09:41 AM
Hello Martin Roussel,

Orders is a collection property of the data items that the main grid is bound to. Let's imagine that RadGridView is bound to a list of Product items:

public class Product
{
    public int UnitPrice { get; set; }
    public List<Order> Orders { get; }
}

The product class has a number of properties, which are displayed in the main grid. In addition to those properties there is one called Orders which is a list of all related orders. If I would like to create a hierarchy using this data you could do something like:

<telerik:RadGridView.HierarchyChildTemplate
               <DataTemplate
                   <telerik:RadGridView x:Name="RadGridView1" CanUserFreezeColumns="False" AutoGenerateColumns="False" ItemsSource="{Binding Orders}"  ShowGroupPanel="False" IsReadOnly="True"
                        
                   </telerik:RadGridView
               </DataTemplate
           </telerik:RadGridView.HierarchyChildTemplate>

This instructs all child grids to get their data from the Orders property of the respective (parent) Product object. Alternatively, you can set up this parent-child relationship using TableDefinition:

<telerik:RadGridView x:Name="mainGrid">
    <telerik:RadGridView.ChildTableDefinitions>
        <telerik:GridViewTableDefinition>
            <telerik:GridViewTableDefinition.Relation>
                <data:PropertyRelation ParentPropertyName="Orders"/>
            </telerik:GridViewTableDefinition.Relation>
        </telerik:GridViewTableDefinition>
    </telerik:RadGridView.ChildTableDefinitions>
</telerik:RadGridView>

When using the latter approach you can use the DataLoading event to customize the child grids. "If Not (dataControl.ParentRow Is Nothing)" will evaluate to true whenever you try to expand a child gird - dataControlParentRow will be different from Nothing for every child grid.


Greetings,
Milan
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
Martin Roussel
Top achievements
Rank 1
Answers by
Milan
Telerik team
Martin Roussel
Top achievements
Rank 1
Share this question
or