Question

I have been banging my head on and on, i cant seem to work around the problem i am facing.

Scenario : I have a SalesOrder Page, I am trying to use this page to create new sales orders, as well as edit existing ones. SalesOrder Page hosts, with other controls that bind to current sales order, RadGridView and DataForm. Both bind to same Observable Collection in MVVM.

The Problem i am facing is that,

1) DataForm and Grid Binds to the collection all right, but Edit Button bound to DataForm Edit Command is Disabled. Only the New Button is enabled.

2) After adding a new Item to the dataform, Edit button is Enabled, BUT no matter which row i have selected, it ALWAYS edit the first row that was Inserted, and unless i insert a row, i cannot Edit any existing rows. It seems like DataForm isnt really aware of any existing rows in the collection !!!

My Implimentation : I am using MVVM-light and following is related chunks of my code:

**ViewModel**

below qsdcvSOPDoc is QueryableSouceDomainCollectionView (a wrapper of domain data source for mvvm), The Sales Order Entity in Model is called SOPDoc

   public const string qsdcvSOPDocPropertyName = "qsdcvSOPDoc";
   private QueryableDomainServiceCollectionView<SOPDoc> _qsdcvSOPDoc;
   public QueryableDomainServiceCollectionView<SOPDoc> qsdcvSOPDoc
    {
        get
        {
            return _qsdcvSOPDoc;
        }

        set
        {
            if (_qsdcvSOPDoc == value)
            {
                return;
            }

            var oldValue = _qsdcvSOPDoc;
            _qsdcvSOPDoc = value;
            RaisePropertyChanged(qsdcvSOPDocPropertyName, oldValue, value, true);

below ocSalesOrderItemsList is Obserable Collection that is populated by the IEnumerable, Child Entity of SOPDoc, where is SOPDoc.SOPDocDetails

    public const string ocSalesOrderItemsListPropertyName = "ocSalesOrderItemsList";
    private ObservableCollection<SOPDocDetail> _ocSalesOrderItemsList;
    public ObservableCollection<SOPDocDetail> ocSalesOrderItemsList
    {
        get
        {
            return _ocSalesOrderItemsList;
        }

        set
        {
            if (_ocSalesOrderItemsList == value)
            {
                return;
            }

            var oldValue = _ocSalesOrderItemsList;
            _ocSalesOrderItemsList = value;
            RaisePropertyChanged(ocSalesOrderItemsListPropertyName, oldValue, value, true);
        }
    }

when qsdcvSOPDoc is loaded with data, entCurrentOrder entity is populated with the current sales order, different controls in View binds to this.

    public const string entCurrentOrderPropertyName = "entCurrentOrder";
    private SOPDoc _entCurrentOrder; public SOPDoc entCurrentOrder
    {
        get
        {
            return _entCurrentOrder;
        }

        set
        {
            if (_entCurrentOrder == value)
            {
                return;
            }
            _entCurrentOrder = value;
            RaisePropertyChanged(entCurrentOrderPropertyName);
        }
    }

PageMode is set before navigating to this page, this is set to either 'New' or 'Edit' and it helps as a check to perform load and save commands as well as trigger some events setting the default values.

    public const string PageModePropertyName = "PageMode";
    private string _pageMode;
    public string PageMode
    {
        get
        {
            return _pageMode;
        }

        set
        {
            if (_pageMode == value)
            {
                return;
            }
            _pageMode = value;
            RaisePropertyChanged(PageModePropertyName);
        }
    }

CurrentSalesOrderId is set while navigating to this page.

    public const string CurrentSalesOrderIdPropertyName = "CurrentSalesOrderId";
    private int _currentSalesOrderId;
    public int CurrentSalesOrderId
    {
        get
        {
            return _currentSalesOrderId;
        }

        set
        {
            if (_currentSalesOrderId == value)
            {
                return;
            }

            var oldValue = _currentSalesOrderId;
            _currentSalesOrderId = value;
            RaisePropertyChanged(CurrentSalesOrderIdPropertyName, oldValue, value, true);
        }
    }

Constructor of ViewModel

 public SalesOrderViewModel()
    {          
            ctx = new KERPDomainContext();
            var ctxDetail = new KERPDomainContext();

            qry = ctx.GetSalesOrderByIdQuery(CurrentSalesOrderId);
            qsdcvSOPDoc = new QueryableDomainServiceCollectionView<SOPDoc>(ctx, qry);
            qsdcvSOPDoc.Load();
            qsdcvSOPDoc.LoadedData += qsdcvSOPDoc_LoadedData;

            ocSalesOrderItemsList = new ObservableCollection<SOPDocDetail>();
            ocSalesOrderItemsList.CollectionChanged += ocSalesOrderItemsList_CollectionChanged; // This will re-calculate the GrossAmount

           //Commands Binding
            SaveSalesOrder = new RelayCommand(SaveSalesOrderExecute, SaveSalesOrderCanExecute);


        }
    }


 void qsdcvSOPDoc_LoadedData(object sender, LoadedDataEventArgs e)
    {

        entCurrentOrder = (SOPDoc)e.Entities.FirstOrDefault();

        if (PageMode == Enums.SODModes.New.ToString() && CurrentSalesOrderId <= 0)
        { // this is a new Sales Order

            if (entCurrentOrder != null)
                ocSalesOrderItemsList.AddRange((entCurrentOrder.SOPDocDetails.Where(i => i.IsActive == true))); // Adds all active items to the collection list

           // setting some values, the property decleration i omitted here for brevity 
           GrossAmount = 0;
            Carriage = 0;
            Discount = 0;

        }


        if (PageMode == Enums.SODModes.Edit.ToString() && CurrentSalesOrderId > 0)
        {
            if (entCurrentOrder != null)
            {
                ocSalesOrderItemsList.AddRange(entCurrentOrder.SOPDocDetails);

                GrossAmount = entCurrentOrder.SOPDocDetails.Sum(i => i.NetAmount);
                Carriage = entCurrentOrder.Carriage;
                Discount = entCurrentOrder.Discount;
            }
        }
    }

View

The View has a RadGridView control and a dataform control both bound to the same ItemSource and CurrentItem:

  <telerik:RadGridView ItemsSource="{Binding ocSalesOrderItemsList}"  Grid.Row="1"                            
                         AutoExpandGroups="True" AutoGenerateColumns="False" ColumnWidth="*" CurrentItem="{Binding entSOPDocDetail}" IsSynchronizedWithCurrentItem="True" IsReadOnly="False">

            <telerik:RadGridView.Columns>
                <telerik:GridViewDataColumn Header="Product Code" DataMemberBinding="{Binding ProductCode}" Width="1.5*"/>
                <telerik:GridViewDataColumn Header="Description" DataMemberBinding="{Binding Description}" Width="5*"/>
                <telerik:GridViewDataColumn Header="Qty" DataMemberBinding="{Binding Qty}" Width="*"/>
                <telerik:GridViewDataColumn Header="UnitType" DataMemberBinding="{Binding UnitType}"/>
                <telerik:GridViewDataColumn Header="Unit Price" DataMemberBinding="{Binding UnitPrice}" DataFormatString="{}{0:0,0.00}"/>
                <telerik:GridViewDataColumn Header="Line Total" DataMemberBinding="{Binding NetAmount}" DataFormatString="{}{0:0,0.00}"/>
                <telerik:GridViewColumn Width="90">
                    <telerik:GridViewColumn.CellTemplate>
                        <DataTemplate>
                            <telerik:RadButton Content="Delete" Command="telerik:RadGridViewCommands.Delete" CommandParameter="{Binding}" />
                        </DataTemplate>
                    </telerik:GridViewColumn.CellTemplate>
                </telerik:GridViewColumn>
            </telerik:RadGridView.Columns>           
    </telerik:RadGridView>

  <telerik:RadDataForm x:Name="dataForm"
                         ItemsSource="{Binding ocSalesOrderItemsList}" 
                         CommandButtonsVisibility="Cancel,Commit" 
                         AutoGenerateFields="False"
                         ValidationSummaryVisibility="Collapsed"
                         EditEnded="RadDataForm_EditEnded"
                         CurrentItemChanged="OnDataFormCurrentItemChanged"
                         LabelPosition="Above"
                         EditTemplate="{StaticResource SOItemsEditTemplate}"
                         NewItemTemplate="{StaticResource SOItemsEditTemplate}" 
                         VerticalContentAlignment="Stretch" HorizontalContentAlignment="Stretch" CurrentItem="{Binding entSOPDocDetail}"/>

Add and Edit buttons are bound to DataForm Commands:

      <telerik:RadButton RenderTransformOrigin="0.5,0.5" Style="{StaticResource smallCommandCircleRadButtons}" Tag="EDIT"
                               Command="telerik:RadDataFormCommands.BeginEdit" CommandTarget="{Binding ElementName=dataForm}" />


      <telerik:RadButton RenderTransformOrigin="0.5,0.5" Style="{StaticResource smallCommandCircleRadButtons}" Margin="0,0,40,0" Tag="ADD ITEM" 
                               Command="telerik:RadDataFormCommands.AddNew" CommandTarget="{Binding ElementName=dataForm}" />

Again - The Problem The Problem i am facing is that, 1) DataForm and Grid Binds to the collection all right, but Edit Button bound to DataForm Edit Command is Disabled. Only the New Button is enabled.

2) After adding a new Item to the dataform, Edit button is Enabled, BUT no matter which row i have selected, it ALWAYS edit the first row that was Inserted, and unless i insert a row, i cannot Edit any existing rows. It seems like DataForm isnt really aware of any existing rows in the collection !!!

I know there may be principle errors in the approach above, or may be the whole approach is wrong, but that is why i am seeking help!! any suggestions even on a different approach suggestions which involves, CRUD, dataform and Grid with MVVM support, would be very appreciated.

Was it helpful?

Solution

Well at last i found a way to approach the above by myself and with the help of another question i posted on SO Here.

what i did actually is, declared a DomainContext ctx and then i loaded it with sales Orders via DomainDataSource1 and another DomainDataSource2 loaded the Order Details. Both of these bound to Grid and Dataform respectively. After all the editing and adding of records, i just called ctx.SaveChanges which saved all the changes to ALL the loaded entities back to Ria.Entity.AcceptChanges() protected method. Thats IT...

CONCLUSION : Doesnt matter how many DomainDataSources we use to load data into the context, if you want to save them all together, use a SINGLE DomainContext for all of them. Only use saperate DomainContexts if you are going to use partial SubmitChanges :).

Still need clarification? comment me .......

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top