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

[Solved] Silverlight grid and MVVM insert / delete

7 Answers 217 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.
eraashishgupta
Top achievements
Rank 1
eraashishgupta asked on 01 Oct 2010, 06:39 PM
Hi all,
<telerik:RadGridView ItemsSource="{Binding Path=CompanySource.View,Mode=TwoWay}" Name="companyRadGridView"
                            AutoGenerateColumns="False" ShowGroupPanel="False" 
                            SelectedItem="{Binding Path=CurrentCompany,Mode=TwoWay}"
                            DataLoadMode="Asynchronous"
                            IsSynchronizedWithCurrentItem="false"
                            Height="125" VerticalAlignment="Top" Grid.ColumnSpan="2">
           <i:Interaction.Triggers>
               <i:EventTrigger EventName="Deleted">
                   <cmd:EventToCommand PassEventArgsToCommand="True" Command="{Binding Path=DeleteItemCommand}"/>
               </i:EventTrigger>
           </i:Interaction.Triggers>
                      <telerik:RadGridView.Columns>
               <telerik:GridViewDataColumn Header="Company Code" DataMemberBinding="{Binding CompanyCode}" />
               <telerik:GridViewDataColumn Header="Company Name" DataMemberBinding="{Binding CompanyName}" />
           </telerik:RadGridView.Columns>
       </telerik:RadGridView>
given above is the xaml i am using MVVM light but the default Insert and Delete keys are not working for me is it because what i am binding to is a read only collection but for the delete i am using it like this

public void RemoveCompany(Company company)
       {
           if (Context.Companies.Contains(company))
               Context.Companies.Remove(company);
       }
private RelayCommand<Company> _deleteItem = null;
 
       public RelayCommand<Company> DeleteItemCommand
       {
           get
           {
               if (_deleteItem == null)
               {
                   _deleteItem = new RelayCommand<Company>(
                       g => this.OnDeleteItemCommand(g),
                       g => !(this._companyModel.HasChanges) && (g != null));
               }
               return _deleteItem;
           }
       }
 
       private void OnDeleteItemCommand(Company g)
       {
           try
           {
               if (!_companyModel.IsBusy)
               {
                   
                   if (_companyModel.HasChanges)
                   {
                       this._companyModel.RejectChanges();
                   }
 
                   
                   DialogMessage dialogMessage = new DialogMessage(
                       this,
                       Resources.DeleteCurrentUserMessageBoxText,
                       s =>
                       {
                           if (s == MessageBoxResult.OK)
                           {
                               
                               _companyModel.RemoveCompany(g);
 
                               
                               this._companyModel.SaveChangesAsync();
                           }
                       })
                   {
                       Button = MessageBoxButton.OKCancel,
                       Caption = Resources.ConfirmMessageBoxCaption
                   };
 
                   AppMessages.PleaseConfirmMessage.Send(dialogMessage);
               }
           }
           catch (Exception ex)
           {
               
               AppMessages.RaiseErrorMessage.Send(ex);
           }
       }

Can you please let me know why the events are not firing.
Thanks,
Aashish Gupta

7 Answers, 1 is accepted

Sort by
0
eraashishgupta
Top achievements
Rank 1
answered on 01 Oct 2010, 09:47 PM
Ok , 
Changing the binding of the radgrid in the viewmodel from CollectionViewSource to IList did the trick.
And now i can insert and delete stuffs but yeah there are still some problems which i am facing using insert and delete i will update you with the final solution as soon as i reach there , inbetween if you have some other suggestion or if you think that IList is not thing to do then you can suggest me some other mechanism.
EntityCollectiomView was not fitting my scenario so i havn't implemented it.

Thanks a lot.
Aashish Gupta
0
Gerry Connolly
Top achievements
Rank 1
answered on 02 Oct 2010, 08:51 AM
Yeah i am also facing the same problem.
Can anyone propose a solution for this.

Thanks
0
eraashishgupta
Top achievements
Rank 1
answered on 02 Oct 2010, 08:56 AM
Hi after changing also to the IList the basic problem exists that is i am loosing the ria tracking of entity things because i have to manage the entity insert and delete manually which is again creating a lot of trouble.
I think i have to apply EntityCollectionView but i am unable to convert my IEnumerable to EntityCollectionView.

I am using this to perform query
private void PerformQuery<T>(EntityQuery<T> qry, EventHandler<EntityResultsArgs<T>> evt) where T : Entity
       {
           Context.Load<T>(qry, LoadBehavior.RefreshCurrent, r =>
           {
               if (evt != null)
               {
                   try
                   {
                       if (r.HasError)
                       {
                           evt(this, new EntityResultsArgs<T>(r.Error));
                           r.MarkErrorAsHandled();
                       }
                       else
                       {
                           evt(this, new EntityResultsArgs<T>(r.Entities));
                       }
                   }
                   catch (Exception ex)
                   {
                       evt(this, new EntityResultsArgs<T>(ex));
                   }
               }
           }, null);
       }

and then in the completed event which is in another layer i do

private void _companyModel_GetCompaniesComplete(object sender, EntityResultsArgs<Company> e)
        {
            if (!e.HasError)
            {
                 
                if (_companyModel.HasChanges)
                {
                    AppMessages.CancelChangesMessage.Send();
                }
 
                this._companies = e.Results.OrderBy(g => g.CompanyName);
                CompanyView = _companies.ToList();
                                
                RaisePropertyChanged("CompanyView");
                // set the first row as the current issue
                if (e.Results.Count() >= 1)
                {
                    var enumerator = _companies.GetEnumerator();
                    enumerator.MoveNext();
                    CurrentCompany = enumerator.Current;
 
                    AppMessages.EditCompanyMessage.Send(CurrentCompany);
                }
            }
            else
            {
                // notify user if there is any error
                AppMessages.RaiseErrorMessage.Send(e.Error);
            }
        }
So my question is how can i change it to EntityCollectionview

Thanks
Aashish Gupta
0
eraashishgupta
Top achievements
Rank 1
answered on 02 Oct 2010, 09:03 AM
This is what the EntityResultArgs look like
public class EntityResultsArgs<T> : ResultsArgs where T : Entity
    {
        public EntityResultsArgs(Exception ex)
            : base(ex)
        {
        }
 
        public EntityResultsArgs(IEnumerable<T> results)
            : base(null)
        {
            _results = results;
        }
 
        private IEnumerable<T> _results;
 
        public IEnumerable<T> Results
        {
            get { return _results; }
        }
    }
0
Nedyalko Nikolov
Telerik team
answered on 07 Oct 2010, 06:06 AM
Hello eraashishgupta,

I cannot give you any advice how to convert IEnumerable to EntityCollectionView, perhaps you have to create your own collection which behaves like EntityCollectionView (implements ICollectionView, IEnumerable, IEditableCollectionView, INotifyCollectionChanged, INotifyPropertyChanged interfaces).
Could you please send me some more information about the root problem? If you could send me a sample project which demonstrates the problem and I can debug on my side will be great. Thank you in advance.

Regards,
Nedyalko Nikolov
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
0
eraashishgupta
Top achievements
Rank 1
answered on 07 Oct 2010, 09:02 AM
Hi Nedyalko,

I can email you the sample code, if you could please give me the email id where to email the code.

Thanks,
Aashish Gupta
0
Vlad
Telerik team
answered on 07 Oct 2010, 09:10 AM
Hi,

 You can open support ticket and attach the sample. 

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
eraashishgupta
Top achievements
Rank 1
Answers by
eraashishgupta
Top achievements
Rank 1
Gerry Connolly
Top achievements
Rank 1
Nedyalko Nikolov
Telerik team
Vlad
Telerik team
Share this question
or