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

Calculated Column using MVVM

4 Answers 322 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Koren
Top achievements
Rank 1
Koren asked on 17 Jan 2011, 06:49 PM
I have two columns in my grid that are calculated using other columns.  Because I need the totals for my database update, I decided to do the calculations in my view model and set the property as changed when these values are updated.  This works perfectly in edit mode.  If I modify my price, the new value gets updated and displays in the grid correctly.  However, I am getting different results for a new record.  If I leave the value column as read only, the display never gets updated even though the value field in my view model is correctly updated.  If I take off the read only setting on the value column, the display does update but only if I give that column focus.  I don't have to do this in Edit mode so I am not sure how to fix this for insert mode.

thanks!

4 Answers, 1 is accepted

Sort by
0
Maya
Telerik team
answered on 20 Jan 2011, 12:55 PM
Hi Koren,

In order to provide you with an appropriate solution, I would need a bit more details. How do you calculate the values in the last column ? Do you use a GridViewExpressionColumn or do you follow an approach similar to this one ? May you verify if the CellEditEnded event is fired when filling up the values in the new item ? It would be great if you share any relevant code-snippets or send us a sample project simulating the issue.
 

All the best,
Maya
the Telerik team
Let us know about your Windows Phone 7 application built with RadControls and we will help you promote it. Learn more>>
0
Koren
Top achievements
Rank 1
answered on 20 Jan 2011, 04:14 PM
I tried the GridViewExpressionColumn but had the same problem on an insert.  I also need the value to update the database so I decided to just use my view model.  Edit works great but the Insert does not show the updated value.
thx, Koren

Here is the relevant parts of the view model:

 

        private void UpdateValue()
        {
            this.Value = this.Acres * this.YieldPerAcre * this.UnitPrice;
        }      
       public double Acres
        {
            get { return _cropProductionRecord.Acres; }
            set
            {
                _cropProductionRecord.Acres = value;
                OnPropertyChanged("Acres");
                UpdateValue();
            }
        }
        public double YieldPerAcre
        {
            get { return _cropProductionRecord.YieldPerAcre; }
            set
            {
                _cropProductionRecord.YieldPerAcre = value;
                OnPropertyChanged("YieldPerAcre");
                UpdateValue();
            }
        }
  
        public double UnitPrice
        {
            get { return _cropProductionRecord.UnitPrice; }
            set
            {
                _cropProductionRecord.UnitPrice = value;
                OnPropertyChanged("UnitPrice");
                UpdateValue();
            }
        }
        public double Value
        {
            get { return _cropProductionRecord.Value; }
            set
            {
                _cropProductionRecord.Value = value;
                OnPropertyChanged("Value");
            }
        }
  
And the grid:
        <telerik:RadGridView DockPanel.Dock="Top" x:Name="grdCropProduction" ItemsSource="{Binding CropProductionRecords}" AutoGenerateColumns="False"
                                         IsFilteringAllowed="True" ShowGroupPanel="False" SelectionMode="Extended" ShowInsertRow="True" 
                                         ActionOnLostFocus="None"  CanUserInsertRows="True" CanUserDeleteRows="True" CanUserReorderColumns="False"
                                         IsReadOnly="False" CanUserFreezeColumns="False" CanUserResizeColumns="False"
                                         Deleting="grdCropProduction_Deleting" RowIndicatorVisibility="Collapsed"
                                         RowEditEnded="grdCropProduction_RowEditEnded"
                                         AddingNewDataItem="grdCropProduction_AddingNewDataItem">
            <telerik:RadGridView.Columns>
                <telerik:GridViewColumn>
                    <telerik:GridViewColumn.CellTemplate>
                        <DataTemplate>
                            <telerik:RadButton Content="Delete" Command="telerikGrid:RadGridViewCommands.Delete" CommandParameter="{Binding}" />
                        </DataTemplate>
                    </telerik:GridViewColumn.CellTemplate>
                </telerik:GridViewColumn>
                <telerik:GridViewComboBoxColumn UniqueName="TransactionCode" Header="ATIGD" IsComboBoxEditable="True" 
                                                DataMemberBinding="{Binding TransactionCodeID}" 
                                                DisplayMemberPath="CodeDescription" 
                                                SelectedValueMemberPath="TransactionCodeID"
                                                >
                    <telerik:GridViewComboBoxColumn.EditorStyle>
                        <Style TargetType="telerik:RadComboBox">
                            <Setter Property="OpenDropDownOnFocus" Value="true" />
                        </Style>
                    </telerik:GridViewComboBoxColumn.EditorStyle>
                </telerik:GridViewComboBoxColumn>
  
                <telerik:GridViewComboBoxColumn Header="Ent #" IsComboBoxEditable="True" 
                                                DataMemberBinding="{Binding FarmYearEnterpriseID}" 
                                                DisplayMemberPath="CodeDescription" 
                                                SelectedValueMemberPath="FarmYearEnterpriseID"
                                                ItemsSource="{Binding FarmYearEnterprises}">
                    <telerik:GridViewComboBoxColumn.EditorStyle>
                        <Style TargetType="telerik:RadComboBox">
                            <Setter Property="OpenDropDownOnFocus" Value="true" />
                        </Style>
                    </telerik:GridViewComboBoxColumn.EditorStyle>
                </telerik:GridViewComboBoxColumn>
  
                <telerik:GridViewDataColumn Header="O/R" DataMemberBinding="{Binding Owned}">
                    <telerik:GridViewDataColumn.CellTemplate>
                        <DataTemplate>
                            <TextBlock Text="{Binding Owned, Converter={StaticResource BooleanConverter}, ConverterParameter=OWNED}" />
                        </DataTemplate>
                    </telerik:GridViewDataColumn.CellTemplate>
                </telerik:GridViewDataColumn>                
                <telerik:GridViewDataColumn Header="Opr %" DataMemberBinding="{Binding OperatorPercent}" DataFormatString="{}{0:p}" IsFilterable="False" />
                <telerik:GridViewDataColumn Header="Acres" Width="60" DataMemberBinding="{Binding Acres}" IsFilterable="False" />
                <telerik:GridViewDataColumn Width="60" DataMemberBinding="{Binding YieldPerAcre}" IsFilterable="False">
                    <telerik:GridViewDataColumn.Header>
                        <TextBlock Text="Yield Per Acre" TextWrapping="Wrap" />
                    </telerik:GridViewDataColumn.Header>
                </telerik:GridViewDataColumn>
                <!--<telerik:GridViewDataColumn Header="QuantityCodeID" DataMemberBinding="{Binding QuantityCodeID}" IsFilterable="False" />-->
                <telerik:GridViewDataColumn Width="60" DataMemberBinding="{Binding UnitPrice}" DataFormatString="{}{0:c}" IsFilterable="False">
                    <telerik:GridViewDataColumn.Header>
                        <TextBlock Text="Unit Price" TextWrapping="Wrap" />
                    </telerik:GridViewDataColumn.Header>
                </telerik:GridViewDataColumn>
                <!--<telerik:GridViewExpressionColumn Header="$ Value" UniqueName="TotalValue" DataFormatString="{}{0:c}"  />-->
                <telerik:GridViewDataColumn Header="$ Value" Width="60" DataMemberBinding="{Binding Value}" DataFormatString="{}{0:c}" IsFilterable="False" IsReadOnly="True" />
                <telerik:GridViewDataColumn Width="60" DataMemberBinding="{Binding UnitWeight}" IsFilterable="False">
                    <telerik:GridViewDataColumn.Header>
                        <TextBlock Text="Unit Weight" TextWrapping="Wrap" />
                    </telerik:GridViewDataColumn.Header>
                </telerik:GridViewDataColumn>
                <telerik:GridViewDataColumn Header="Tot Units" Width="60" DataMemberBinding="{Binding TotalUnits}" IsFilterable="False" />
                <!--<telerik:GridViewExpressionColumn Header="Weight" UniqueName="TotalWeight" />-->
                <telerik:GridViewDataColumn Header="Weight" Width="60" DataMemberBinding="{Binding Weight}" IsFilterable="False" IsReadOnly="True" />
            </telerik:RadGridView.Columns>
        </telerik:RadGridView>

 

 

 

 

 

 

 

 

 

 

 

 

0
Maya
Telerik team
answered on 25 Jan 2011, 04:40 PM
Hi Koren,

Based on the code-snippets provided, I prepared a small application in an attempt to reproduce your scenario. However, I am not able to get the behavior you specified. May you take a look at my application and let me know in case of any discrepancies according to your requirements ? Feel free to change the sample in the way you want and send it back if necessary.
 

All the best,
Maya
the Telerik team
Let us know about your Windows Phone 7 application built with RadControls and we will help you promote it. Learn more>>
0
Koren
Top achievements
Rank 1
answered on 27 Jan 2011, 06:39 PM
I just went from a trial version to the latest version and it fixed the problem.  Must have been an issue in the last version.  thanks for the help!
Tags
GridView
Asked by
Koren
Top achievements
Rank 1
Answers by
Maya
Telerik team
Koren
Top achievements
Rank 1
Share this question
or