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

[Solved] Updating Hierarchical RadGridView + MVVM

4 Answers 261 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.
Meehir
Top achievements
Rank 1
Meehir asked on 28 Dec 2010, 02:28 PM
Hi All,

I am using Telerik RadGridView for displaying hierarchical data using MVVM model, for eg: Order and OrderDetail.  I have got lots of sample code from this website and somehow I succeed in displaying the same.

I can insert new row to parent as well as child grid, but the newly inserted row of child row is not updating the corresponding binding object, even if I put it as TwoWay mode.

The structure of binding object is:
1. Order {List<OrderDetail>, OrderId, OrderDate, etc}
2. OrderDetail {OrderDetailId, OrderId, etc, }

The itemsource of parent GridView is of type List<Order>, and Itemsource of child Gridview is of type List<OrderDetail>.  The binding object has both get, and set property.

Can I get some sample project, where in, it uses Hierarchical RadGridView with functionality as below?
 - insert a new row in child grid and updating the same in binding object of parent grid
 - amend existing row in child grid and updating the same in binding object of parent grid
 - insert a new row in parent grid (This is working at my end)
 - amend a new row in parent grid (This is working at my end)

I am struggling with this from last two days.  I would like to thanks in advance.

Regards,
Meehir Jha

4 Answers, 1 is accepted

Sort by
0
Maya
Telerik team
answered on 28 Dec 2010, 03:12 PM
Hi Meehir,

I am sending you a sample project illustrating a possible implementation for achieving the desired result.
I hope that helps.
 

Regards,
Maya
the Telerik team
Browse the videos here>> to help you get started with RadControls for Silverlight
0
Meehir
Top achievements
Rank 1
answered on 29 Dec 2010, 10:58 AM
Thanks Maya for the great resource... but somehow, it is of less use for me or was not able to understand your project. 

As I said earlier, I have 2 entity and having 1-many relationship like order -> order_detail in your case.  The main gridview's itemsource is order and rowdetail's itemsource is order_detail with static resource as viewmodel. 

Below as sample xaml

...
<UserControl.Resources>
    <my:XYZViewModel x:Key="MyViewModel" />

    <DataTemplate x:Key="OrderDetailTemplate">
 <telerik:RadGridView ItemsSource="{Binding OrderDetailList, Source={StaticResource MyViewModel}}"
                      SelectedItem="{Binding SelectedOrderDetail, Source={StaticResource MyViewModel}}">
  <telerik:RadGridView.Columns>
   ...
  </telerik:RadGridView.Columns>
 </telerik:RadGridView>
    </DataTemplate>
</UserControl.Resources>

<Grid>
    <telerik:RadGridView ItemsSource="{Binding OrderList, Mode=TwoWay}" 
    RowDetailsTemplate="{StaticResource OrderDetailTemplate}"
    SelectedItem="{Binding SelectedOrder, Mode=TwoWay}">
 <telerik:RadGridView.Columns>
   ...
 </telerik:RadGridView.Columns>
    </telerik:RadGridView>
...


Below is sample ViewModel code:

public ObservableCollection<Order> OrderList
{
    get { return _orderList; }
    set
    {
        _orderList = value;
        OnPropertyChanged(() => OrderList);
    }

public ObservableCollection<orderDetail> OrderDetailList
{
    get
    {
        if(SelectedOrder != null)
             _orderDetailList = new ObservableCollection<OrderDetail>(
                                                from item in OrderList.SelectMany(c => c.OrderDetails)
                                                where item.OrderId == SelectedOrder.OrderId
                                                select item);
        else
             _orderDetailList = null;

        return _orderDetailList;
    }
}

public Order SelectedOrder
{
    get { return _selectedOrder; }
    set { _selectedOrder = value; OnPropertyChanged (()=>SelectedOrder); }
}

public OrderDetail SelectedOrderDetail
{
    get { return _selectedOrderDetail; }
    set { _selectedOrderDetail = value; OnPropertyChanged (()=>SelectedOrderDetail); }
}

Problem: When I press insert button on main grid, a new row is added to the bottom of the grid and to the OrderList property which is defined as a binding itemsource of main grid.
When I press insert on child grid, a new row is added to the bottom of the child grid but neither to the OrderDetailList property which is defined as a binding itemsource of child grid nor to OrderList property which is defined as a binding itemsource of main grid

I want some mechanism (event) through which I can fetch newly inserted row of child grid and add it to OrderList property.  If i get all new records (both order and order_detail) to OrderList, then I can filter and send it to service for updations in database.

Thanks in advance.
Meehir Jha

0
Maya
Telerik team
answered on 29 Dec 2010, 01:54 PM
Hi Meehir,

I have tested the issue you specified on the project I previously attached. However, I was not able to reproduce it.
Generally, you may use the RowEditEnded event of the grid to check if a new item is added:

private void customersGridView_RowEditEnded(object sender, GridViewRowEditEndedEventArgs e)
{
    if(e.EditOperationType == Telerik.Windows.Controls.GridView.GridViewEditOperationType.Insert)
    {
        var count = (e.OriginalSource as RadGridView).Items.Count;
    }
}
 
Please give it a try and let me know if it helps.
 

Greetings,
Maya
the Telerik team
Browse the videos here>> to help you get started with RadControls for Silverlight
0
Meehir
Top achievements
Rank 1
answered on 29 Dec 2010, 03:03 PM
Hi Maya,

Thanks for the response.  Actually, I tried with AddingNewItem event and it worked.

Regards,
Meehir Jha
Tags
GridView
Asked by
Meehir
Top achievements
Rank 1
Answers by
Maya
Telerik team
Meehir
Top achievements
Rank 1
Share this question
or