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

Databinding with a identity column

3 Answers 86 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Needle
Top achievements
Rank 1
Needle asked on 26 Mar 2010, 06:35 PM
Hi,

I have a problem in a Grid, editing a new row previously entered. 
The sequence is this :

1. First I insert a new row. 

The code in AddingNewDataItem Event Handler of the Grid is :

        public void EUGruppoGrid_AddingNewDataItem(object sender, Telerik.Windows.Controls.GridView.GridViewAddingNewEventArgs e)
        {
            if (e.Cancel) return;
            String TipoCurrentItem;
            GridViewDataControl GridViewDataControl = e.OwnerGridViewItemsControl;
            if (GridViewDataControl.CurrentItem != null)
                TipoCurrentItem = ((EUGruppoVO)GridViewDataControl.CurrentItem).Tipo;
            else
                TipoCurrentItem = " ";
            e.NewObject = new EUGruppoVO(0, "", TipoCurrentItem);
        }

        the "EUGruppoVO" Constructor is :

public EUGruppoVO(Int32 id, String descrizione, String tipo)
{
this._id = id;  // in the database the fields is a identity primary key
this._descrizione = descrizione;
this._tipo = tipo;
}

the "EUGruppoVO" implements "INotifyPropertyChanged"
the "EUGruppoItemsSource" is an instance of : "public class EUGruppoVOList : ObservableCollection<EUGruppoVO>"

2. Then I move to another row ( the RowEditEnded Event fire with "e.EditOperationType == GridViewEditOperationType.Insert" )The code in RowEditEnded Event Handler of the Grid is :
public void EUGruppoGrid_RowEditEnded(object sender, Telerik.Windows.Controls.GridViewRowEditEndedEventArgs e){if (e.EditAction == GridViewEditAction.Cancel) return;if (e.EditOperationType == GridViewEditOperationType.Insert)_eUGruppoService.Insert((EUGruppoVO)e.NewData);else_eUGruppoService.Update((EUGruppoVO)e.NewData);}

3. Then I move in the row previously entered, strangely the value of the "id" property of the current item has remained zero.I edit the row, when I move to another row the RowEditEnded Event fire again with "e.EditOperationType == GridViewEditOperationType.Update"but the "id" property of (EUGruppoVO)e.NewData is still zero, why ?

An extract of the XAML code is :...<telerikGridView:RadGridView x:Name="EUGruppoGrid" AutoGenerateColumns="False" CanUserFreezeColumns="False" ShowGroupPanel="True" ItemsSource="{Binding EUGruppoItemsSource, Mode=TwoWay}" CurrentItem="{Binding EUGruppoCurrentItem, Mode=TwoWay}" Margin="0,0,0,0"><telerikGridView:RadGridView.Columns><telerikGridView:GridViewComboBoxColumn DataMemberBinding="{Binding Tipo, Mode=TwoWay}" ItemsSource="{Binding TipoEUList, Mode=OneWay}" DisplayMemberPath="Descrizione" SelectedValueMemberPath="Codice" Header="Tipo" IsReadOnly="False" /><telerikGridView:GridViewDataColumn DataMemberBinding="{Binding Descrizione, Mode=TwoWay}" Header="Descrizione" IsReadOnly="False" /><telerikGridView:GridViewDataColumn IsVisible="False" DataMemberBinding="{Binding Id, Mode=OneWay}" Header="Id" IsReadOnly="True" /></telerikGridView:RadGridView.Columns></telerikGridView:RadGridView>...
An extract of the code behind is :...this.EUGruppoGrid.AddingNewDataItem += new EventHandler<Telerik.Windows.Controls.GridView.GridViewAddingNewEventArgs>(_tabelleViewModel.Controller.EUGruppoGrid_AddingNewDataItem);this.EUGruppoGrid.RowEditEnded += new EventHandler<Telerik.Windows.Controls.GridViewRowEditEndedEventArgs>(_tabelleViewModel.Controller.EUGruppoGrid_RowEditEnded);...DataContext = _tabelleViewModel

Thanks, Dario.

3 Answers, 1 is accepted

Sort by
0
Nedyalko Nikolov
Telerik team
answered on 01 Apr 2010, 10:54 AM
Hi Dario Cristante,

If I understand correctly you are creating ObservableCollection from your database and pass it as an ItemsSource of the grid. Unfortunately database does not update this ObservableCollection when somebody has inserted or edited a record in it, you have to do it manually. So on RadGridView.RowEditEnded event you also have to update RadGridView.ItemsSource. Let me know if there is something unclear or I'm not correct in my guess.


Greetings,
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
Needle
Top achievements
Rank 1
answered on 06 Apr 2010, 10:51 AM
Hi Nikolov, it seems that I solved my problem with this code lines :

        public void EUGruppoGrid_RowEditEnded(object sender, Telerik.Windows.Controls.GridViewRowEditEndedEventArgs e)
        {
            if (e.EditAction == GridViewEditAction.Cancel) return;

            if (e.EditOperationType == GridViewEditOperationType.Insert)
            {
EUGruppoVO EUGruppoNew = _eUGruppoInService.Insert((EUGruppoVO)e.NewData);
                ((EUGruppoVO)e.NewData).Id = EUGruppoNew.Id;
            }
            else
            {
                _eUGruppoInService.Update((EUGruppoVO)e.NewData);
            }
        }

Now "id" property is correctly valued, all works fine but this workaround is right ?

Thanks Dario.

0
Stefan Dobrev
Telerik team
answered on 09 Apr 2010, 09:33 AM
Hello Dario,

Yes, this is the correct way to implement this.

Best wishes,
Stefan Dobrev
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
Needle
Top achievements
Rank 1
Answers by
Nedyalko Nikolov
Telerik team
Needle
Top achievements
Rank 1
Stefan Dobrev
Telerik team
Share this question
or