Update Cell value From another Cell value in datagrid

时间:2017-05-16 09:38:42

标签: c# wpf

I want to Update a cell value according to another cell in datagrid. for example i have a quantity column and a total price column when i change the quantity i trigger a CellEditEnding event, which takes the quantity and do some logic operation and save it in the totalpricecolumn of the row itself. after that, the totalprice not changing, only after i click the totalprice cell for editing mode, than i see the result.

my question is: how can see the result right after the CellEditEnding completes?

here's my xaml:

<DataGrid Name="itemsInSheet" CanUserAddRows="False" PreviewTextInput="PreviewQuantityChanged" CellEditEnding="itemsInSheet_CellEditEnding" ItemsSource="{Binding ItemsInPricingSheet , Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Grid.Column="1" Grid.Row="1" Panel.ZIndex="-1" HorizontalAlignment="Center" AutoGenerateColumns="False">
        <DataGrid.Columns>
            <DataGridTextColumn Header="סה''כ מחיר" Binding="{Binding TotalPrice, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
            <DataGridTextColumn Header="כמות" Binding="{Binding Quantity, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
            <DataGridTextColumn Header="שם פריט" IsReadOnly="True" Binding="{Binding ItemID, Mode=TwoWay, Converter={StaticResource ItemIDToItemName}}"/>
            <DataGridTextColumn Header="שם קבוצה" IsReadOnly="True" Binding="{Binding GroupID, Mode=TwoWay, Converter={StaticResource GroupIDToGroupName}}"/>
        </DataGrid.Columns>
    </DataGrid>

Here's my CellEditEnding event:

private void itemsInSheet_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e)
    {
        if (e.EditAction == DataGridEditAction.Commit)
        {
            var column = e.Column as DataGridBoundColumn;
            if (column != null)
            {                    
                var bindingPath = (column.Binding as Binding).Path.Path;
                if (bindingPath == "Quantity")
                {                       
                    int rowIndex = e.Row.GetIndex();
                    var el = e.EditingElement as TextBox;
                    m_engine.UpdateTotalPrice(rowIndex);

                }
            }
        }
    }

And here's the UpdateTotalPrice (for now i keep it simple and the total price equal to the Quantity):

internal void UpdateTotalPrice(int rowIndex)
    {
       sp_GetItemsInPricingSheets_Result data = m_PricingSheetManagerViewModel.ItemsInPricingSheet.ElementAt(rowIndex);
       data.TotalPrice = (decimal)data.Quantity;
    }

ADDITIONAL INFO: here's the viewmodel i'm using:

 public class PricingSheetManagerViewModel : INotifyPropertyChanged
{
    private ObservableCollection<sp_GetGroups_Result> m_groups;
    public ObservableCollection<sp_GetGroups_Result> Groups
    {
        get { return m_groups; }
        set
        {
            m_groups = value;
            NotifyPropertyChanged("Groups");
        }
    }

    private ObservableCollection<sp_GetItems_Result> m_items;
    public ObservableCollection<sp_GetItems_Result> Items
    {
        get { return m_items; }
        set
        {
            m_items = value;
            NotifyPropertyChanged("Items");
        }
    }

    private ObservableCollection<sp_GetItemsInPricingSheets_Result> m_itemsInPricingSheet;
    public ObservableCollection<sp_GetItemsInPricingSheets_Result> ItemsInPricingSheet
    {
        get { return m_itemsInPricingSheet; }
        set
        {
            m_itemsInPricingSheet = value;
            NotifyPropertyChanged("ItemsInPricingSheet");
        }
    }

    private sp_GetPricingSheets_Result m_pricingSheet = new sp_GetPricingSheets_Result();
    public sp_GetPricingSheets_Result PricingSheet
    {
        get { return m_pricingSheet; }
        set
        {
            m_pricingSheet = value;
            NotifyPropertyChanged("PricingSheet");
        }
    }

    #region "PropertyChanged Event"
    public event PropertyChangedEventHandler PropertyChanged;

    private void NotifyPropertyChanged(string propertyName)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }
    #endregion   
}

1 个答案:

答案 0 :(得分:0)

我通过聚焦数据网格,将当前单元格设置为所需单元格并设置开始编辑后,我设法看到该项目 我在itemsInSheet_CellEditEnding事件中添加了这个 这是更新后的代码:

{{1}}