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

[Solved] Rebind() ???

6 Answers 199 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.
Andrew
Top achievements
Rank 1
Andrew asked on 27 Jul 2011, 05:48 PM

I have a grid with multiple levels.
In other words using the 
RowDetailsTemplate i put a tab control in in which i have the details and a grid with objects attached to the original entity and then within that i do the same again.

It works very well when editing any item. 

To add a new item anywhere within the hierarchy i popup a child window where the user can fill out the details and then simply saves the data to the database. To the child window i only pass the owner id in other words when the save button is pressed the data is saved but the grid does not know about it. It needs to be reloaded to see the result.

I use the Rebind() method. However it is not ideal, because when i do that the whole grid structure gets reloaded and i lose my place in the structure. The user has to again drill down into where they were just a minute ago.

Any ideas how i can prevent that? Perhaps some way to keep the grid open as it was before even after a Rebind() or just a better way of doing it all together..


 

6 Answers, 1 is accepted

Sort by
0
Maya
Telerik team
answered on 28 Jul 2011, 09:53 AM
Hello Andrew,

In order to provide you with an appropriate solution, I would need a bit more details. How do you save the data ? Do you add the newly created item to the Items collection of the grid and then save the changes to the data base ? What is the type of the ItemsSource of the grid ? 
 

Best wishes,
Maya
the Telerik team

Register for the Q2 2011 What's New Webinar Week. Mark your calendar for the week starting July 18th and book your seat for a walk through of all the exciting stuff we will ship with the new release!

0
Andrew
Top achievements
Rank 1
answered on 28 Jul 2011, 10:35 PM
No i don't.

The new child window does all the saving itself. and once it has done so it simply closes the new window.
Something like this:

public NewPersonViewModel(int ownerId)
        {
            if (!DesignerProperties.IsInDesignTool)
            {
                _ownerId = ownerId;
  
                SaveNewRecordCommand = new DelegateCommand(SaveNewRecord, CanSaveNewRecord);
  
                EntityQuery<Person> getQuery = _context.GetPeopleByOfficeIdQuery(_ownerId);
                _people = new QueryableDomainServiceCollectionView<Person>(_context, getQuery);
                _people.AutoLoad = true;
                _people.SubmittedChanges += new EventHandler<Telerik.Windows.Controls.DomainServices.DomainServiceSubmittedChangesEventArgs>(_records_SubmittedChanges);
  
                _newPersonDetails = new PersonDetails();
            }
        }
  
        private QueryableDomainServiceCollectionView<Person> _people;
  
        public IEnumerable People
        {
            get { return _people; }
        }
  
        private PersonDetails _newPersonDetails;
  
        public PersonDetails NewPersonDetails
        {
            get { return _newPersonDetails; }
            set
            {
                if (value != _newPersonDetails)
                {
                    _newPersonDetails = value;
                    OnPropertyChanged("NewPersonDetails");
                }
            }
        }
  
        private void SaveNewRecord(object param)
        {
            Person myRecord = new Person();
  
           ...
  
            _people.AddNew(myRecord);
            _people.SubmitChanges();
  
        }


0
Maya
Telerik team
answered on 03 Aug 2011, 10:25 AM
Hi Andrew,

I have tried to reproduce the issue you reported, but without any success. I have updated the sample attached in this blog post with inserting the logic for adding a new item. However, this newly created item is displayed right away. 
Please take a look at the sample attached and let me know in case of any misunderstandings. 

Regards,
Maya
the Telerik team

Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get now >>

0
Andrew
Top achievements
Rank 1
answered on 03 Aug 2011, 11:02 AM

I had a look at this sample you send me. But it bears no resemblance to what i described.

Again, I have a GridView, when I press a button a child window is open, the grid view passes an id to the child window, I then enter data and save the new data to the database using the ID as a reference of where to save it. I then close the child window. At this point the GridView needs to be reloaded because it does not know data has changed. But because I reload it, it loses its place. In other words it reloads at the top.

The solution:

A)    Either I manage to rebind or reload it in such a way that the grid remains in the same position it was it.
B)    Or I somehow manage to add the item to the grid at the same time as it is saved to the database.


I am not sure how to do either of those.

Perhaps if you had a sample on how one should use a child window while adding a new item to a RadGridView that would solve my problem.

0
Maya
Telerik team
answered on 05 Aug 2011, 10:24 AM
Hi Andrew,

My idea was to illustrate how you may insert a new item and display it without calling Rebind().Generally, neither the domain data source, not the grid takes into account whether this item is added through a button or a window. You may take a look at this forum thread for a reference on a possible approach for inserting an item in the grid with a window. 
You may take a look at this forum thread for a hint how to keep the scroll position if necessary.
 

Regards,
Maya
the Telerik team

Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get now >>

0
Andrew
Top achievements
Rank 1
answered on 09 Aug 2011, 04:18 PM

Hi Maya,

I used your sample with the telerik window to come up with a Child Window solution.

Basically the changes i made were moving the saving mechanism from my ChildWindow's ViewModel into the Parent.
I broke my MVVM pattern, but will come up with how to fix it later.

In the meantime however all i managed to do it to fix the problem at the highest level.

So in other words when i add a new Organisation, which is on the top level of my gridview hierarchy, it works fine. Simply because I add a new item to the _organisations which is bound to the grid, I then run the _organisation.SubmitChanges(). All perfect. The new item appears and I am still there, not only that it actually selects the new item.

The problem is when I add a new office which is an EntityCollection within my organisation I still have to run the SubmitChanges() on the _organisations, not the _offices. And because of this I lose focus of where I was.

I am attaching my code as it might sound a bit confusing.

private QueryableDomainServiceCollectionView<Organisation> _organisations;
          
        public IEnumerable Organisations
        {
            get { return _organisations; }
        }
private Organisation _selectedOrganisation;
  
        public Organisation SelectedOrganisation
        {
            get { return _selectedOrganisation; }
            set
            {
                if (value != _selectedOrganisation)
                {
                    _selectedOrganisation = value;
                    OnPropertyChanged("SelectedOrganisation");
                }
            }
        }

public void NOT_MVVM_SaveNewClient(Organisation _newOrganisation)
        {
            Organisation myRecord = new Organisation();
              
            //code removed for post
                                                              
            _organisations.AddNew(myRecord);
            _organisations.SubmitChanges();
        }
public void NOT_MVVM_SaveNewOffice(Office _newOffice)
        {
            Office myRecord = new Office();
  
            //code removed for post
  
            _selectedOrganisation.Offices.Add(myRecord);
            _organisations.SubmitChanges();
        }

<telerik:RadGridView x:Name="uxClientList" RowDetailsVisibilityMode="VisibleWhenSelected" AutoGenerateColumns="False" ItemsSource="{Binding Organisations}" SelectedItem="{Binding SelectedOrganisation, Mode=TwoWay}" IsReadOnly="True" VerticalAlignment="Top" ShowGroupPanel="False">
                        <telerik:RadGridView.Columns>
                            <telerik:GridViewDataColumn DataMemberBinding="{Binding Details.Name}" />
                            <telerik:GridViewDataColumn DataMemberBinding="{Binding Details.Description}" />
                            <telerik:GridViewDataColumn DataMemberBinding="{Binding Core.CreationDate}" DataFormatString="d MMMM yyyy" />
                            <telerik:GridViewDataColumn DataMemberBinding="{Binding Core.Position}" />
                        </telerik:RadGridView.Columns>
                        <telerik:RadGridView.RowDetailsTemplate>
                            <DataTemplate>
                                <telerik:RadTabControl>
                                    <telerik:RadTabItem Header="Details">
                                        <telerik:RadDataForm CurrentItem="{Binding}" AutoGenerateFields="False">
                                            <telerik:RadDataForm.ReadOnlyTemplate>
                                                <DataTemplate>
                                                    <StackPanel>
                                                        <telerik:DataFormDataField DataMemberBinding="{Binding Details.Name,Mode=OneWay}" Label="Client Name:" IsEnabled="False" />
                                                        <telerik:DataFormDataField DataMemberBinding="{Binding Details.Description,Mode=OneWay}" Label="Description:" IsEnabled="False"/>
                                                        <telerik:DataFormDataField DataMemberBinding="{Binding WebSite,Mode=OneWay}" Label="Web Site:" IsEnabled="False"/>
                                                        <telerik:DataFormDataField DataMemberBinding="{Binding Details.Notes,Mode=OneWay}" Label="Notes:" IsEnabled="False" />
                                                        <telerik:DataFormComboBoxField DataMemberBinding="{Binding VisibilityTypeId,Mode=OneWay}" Label="Visibility:" DisplayMemberPath="Name" SelectedValuePath="Id" ItemsSource="{Binding Source={StaticResource Clients_VM}, Path=VisibilityTypes}" IsEnabled="False"/>
                                                        <telerik:DataFormDataField DataMemberBinding="{Binding Core.StateFlag,Mode=OneWay}" Label="State Flag:" IsEnabled="False"/>
                                                        <telerik:DataFormDataField DataMemberBinding="{Binding Core.Position,Mode=OneWay}" Label="Position:" IsEnabled="False"/>
                                                        <telerik:DataFormComboBoxField DataMemberBinding="{Binding UserId,Mode=OneWay}" Label="Owner:" DisplayMemberPath="UserGuid" SelectedValuePath="Id" ItemsSource="{Binding Source={StaticResource Clients_VM}, Path=Users}" IsEnabled="False"/>
                                                    </StackPanel>
                                                </DataTemplate>
                                            </telerik:RadDataForm.ReadOnlyTemplate>
                                            <telerik:RadDataForm.EditTemplate>
                                                <DataTemplate>
                                                    <StackPanel>
                                                        <telerik:DataFormDataField DataMemberBinding="{Binding Details.Name,Mode=TwoWay}" Label="Client Name:"/>
                                                        <telerik:DataFormDataField DataMemberBinding="{Binding Details.Description,Mode=TwoWay}" Label="Description:"/>
                                                        <telerik:DataFormDataField DataMemberBinding="{Binding WebSite,Mode=TwoWay}" Label="Web Site:"/>
                                                        <telerik:DataFormDataField DataMemberBinding="{Binding Details.Notes,Mode=TwoWay}" Label="Notes:"/>
                                                        <telerik:DataFormComboBoxField DataMemberBinding="{Binding VisibilityTypeId,Mode=TwoWay}" Label="Visibility:" DisplayMemberPath="Name" SelectedValuePath="Id" ItemsSource="{Binding Source={StaticResource Clients_VM}, Path=VisibilityTypes}" />
                                                        <telerik:DataFormDataField DataMemberBinding="{Binding Core.StateFlag,Mode=TwoWay}" Label="State Flag:" />
                                                        <telerik:DataFormDataField DataMemberBinding="{Binding Core.Position,Mode=TwoWay}" Label="Position:" />
                                                        <telerik:DataFormComboBoxField DataMemberBinding="{Binding UserId,Mode=TwoWay}" Label="Owner:" DisplayMemberPath="UserGuid" SelectedValuePath="Id" ItemsSource="{Binding Source={StaticResource Clients_VM}, Path=Users}" />
                                                    </StackPanel>
                                                </DataTemplate>
                                            </telerik:RadDataForm.EditTemplate>
                                        </telerik:RadDataForm>
                                    </telerik:RadTabItem>
                                    <telerik:RadTabItem Header="Offices">
                                        <StackPanel>
                                            <telerik:RadButton x:Name="uxNewOfficeButton" Content="New Office" Click="uxNewOfficeButton_Click"/>
                                            <telerik:RadGridView x:Name="uxOfficeList" RowDetailsVisibilityMode="VisibleWhenSelected" AutoGenerateColumns="False" ItemsSource="{Binding Offices}" SelectedItem="{Binding Path=SelectedOffice, Source={StaticResource Clients_VM},Mode=TwoWay}" IsReadOnly="True" VerticalAlignment="Top" ShowGroupPanel="False">
                                                <telerik:RadGridView.Columns>
                                                    <telerik:GridViewDataColumn DataMemberBinding="{Binding Details.Name}" />
                                                    <telerik:GridViewDataColumn DataMemberBinding="{Binding Details.Description}" />
                                                    <telerik:GridViewDataColumn DataMemberBinding="{Binding Core.CreationDate}" DataFormatString="d MMMM yyyy" />
                                                    <telerik:GridViewDataColumn DataMemberBinding="{Binding Core.Position}" />
                                                </telerik:RadGridView.Columns>
                                                <telerik:RadGridView.RowDetailsTemplate>
                                                    <DataTemplate>
                                                        <telerik:RadTabControl>
                                                            <telerik:RadTabItem Header="Details">
                                                                <telerik:RadDataForm CurrentItem="{Binding}" AutoGenerateFields="False">
                                                                    <telerik:RadDataForm.ReadOnlyTemplate>
                                                                        <DataTemplate>
                                                                            <StackPanel>
                                                                                <telerik:DataFormDataField DataMemberBinding="{Binding Details.Name,Mode=OneWay}" Label="Office Name:" IsEnabled="False" />
                                                                                <telerik:DataFormDataField DataMemberBinding="{Binding Details.Description,Mode=OneWay}" Label="Description:" IsEnabled="False"/>
                                                                                <telerik:DataFormDataField DataMemberBinding="{Binding Details.Notes,Mode=OneWay}" Label="Notes:" IsEnabled="False" />
                                                                                <telerik:DataFormDataField DataMemberBinding="{Binding Core.StateFlag,Mode=OneWay}" Label="State Flag:" IsEnabled="False"/>
                                                                                <telerik:DataFormDataField DataMemberBinding="{Binding Core.Position,Mode=OneWay}" Label="Position:" IsEnabled="False"/>
                                                                            </StackPanel>
                                                                        </DataTemplate>
                                                                    </telerik:RadDataForm.ReadOnlyTemplate>
                                                                    <telerik:RadDataForm.EditTemplate>
                                                                        <DataTemplate>
                                                                            <StackPanel>
                                                                                <telerik:DataFormDataField DataMemberBinding="{Binding Details.Name,Mode=TwoWay}" Label="Office Name:"/>
                                                                                <telerik:DataFormDataField DataMemberBinding="{Binding Details.Description,Mode=TwoWay}" Label="Description:"/>
                                                                                <telerik:DataFormDataField DataMemberBinding="{Binding Details.Notes,Mode=TwoWay}" Label="Notes:"/>
                                                                                <telerik:DataFormDataField DataMemberBinding="{Binding Core.StateFlag,Mode=TwoWay}" Label="State Flag:" />
                                                                                <telerik:DataFormDataField DataMemberBinding="{Binding Core.Position,Mode=TwoWay}" Label="Position:" />
                                                                            </StackPanel>
                                                                        </DataTemplate>
                                                                    </telerik:RadDataForm.EditTemplate>
                                                                </telerik:RadDataForm>
                                                            </telerik:RadTabItem>
                                                        </telerik:RadTabControl>
                                                    </DataTemplate>
                                                </telerik:RadGridView.RowDetailsTemplate>
                                            </telerik:RadGridView>
                                        </StackPanel>
                                    </telerik:RadTabItem>
                                </telerik:RadTabControl>
                            </DataTemplate>
                        </telerik:RadGridView.RowDetailsTemplate>
                    </telerik:RadGridView>
Tags
GridView
Asked by
Andrew
Top achievements
Rank 1
Answers by
Maya
Telerik team
Andrew
Top achievements
Rank 1
Share this question
or