Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7865913
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T00:13:42+00:00 2026-06-03T00:13:42+00:00

I am using a DataGrid and is bind using an ObservableCollection in the ViewModel

  • 0

I am using a DataGrid and is bind using an ObservableCollection in the ViewModel

private ObservableCollection<StockItem> _stockList;
public ObservableCollection<StockItem> StockList
{
    get
    {
        return _stockList;
    }
    set
    {
        _stockList = value;
        OnPropertyChanged("StockList");                
    }
}

The StockItem class contains its Properties which are the Column in DataGrid.
There is a column named Amount in DataGrid whose values changed with Quantity*Price Column of the same datagrid.

I have a Property called TotalAmount in the ViewModel which is calculated in the ObservableCollection CollectionChanged event like

void OnStockListChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
{
    this.TotalAmount = this.StockList.Sum(t => t.Amount);
}

This values is only updated in the TextBox bind to TotalAmount when a new row is added to a DataGrid with some data. I want this TextBox of TotalAmount to be updated as soon as the Amount Column in the datagrid changes.

How can i do this.

StockItem Class

public class StockItem : ObservableObject, ISequencedObject
{
    JIMSEntities dbContext = new JIMSEntities();
    public StockItem()
    {
        var qs = dbContext.Stocks.Select(s => s.StockName);
        _stocks = new CollectionView(qs.ToArray());
        _stocks.CurrentChanged += new EventHandler(Stocks_CurrentChanged);
    }
    void Stocks_CurrentChanged(object sender, EventArgs e)
    {
        if (_stocks.CurrentItem != null)
            StockName = _stocks.CurrentItem.ToString();
        var qs = (from p in dbContext.Stocks
                  where p.StockName.Contains(StockName)
                  select new { Unit = p.Unit, UnitPrice = p.UnitPrice }).SingleOrDefault();
        if (qs != null)
        {
            Unit = qs.Unit;
            UnitPrice = (decimal)qs.UnitPrice;
        }
    }

    private CollectionView _stocks;
    public CollectionView Stocks
    {
        get
        {
            return _stocks;
        }
        set
        {
            _stocks = value;
            OnPropertyChanged("Stocks");
        }
    }
    private int _sNo;
    public int SNo
    {
        get
        {
            return _sNo;
        }
        set
        {
            _sNo = value;
            OnPropertyChanged("SNo");

        }
    }
    private string _stockName;
    public string StockName
    {
        get
        {
            return _stockName;
        }
        set
        {
            _stockName = value;
            OnPropertyChanged("StockName");
        }
    }
    private decimal _unitPrice;
    public decimal UnitPrice
    {
        get
        {
            return _unitPrice;
        }
        set
        {
            _unitPrice = value;
            OnPropertyChanged("UnitPrice");
            OnPropertyChanged("Amount");
        }
    }
    private string _unit;
    public string Unit
    {
        get
        {
            return _unit;
        }
        set
        {
            _unit = value;
            OnPropertyChanged("Unit");
        }
    }

    private decimal _discount;
    public decimal Discount
    {
        get
        {
            return _discount;
        }
        set
        {
            _discount = value;
            OnPropertyChanged("Discount");
            OnPropertyChanged("Amount");
        }
    }
    private decimal _quantity;
    public decimal Quantity
    {
        get
        {
            return _quantity;

        }
        set
        {
            _quantity = value;
            OnPropertyChanged("Quantity");
            OnPropertyChanged("Amount");
        }
    }
    public decimal Amount
    {
        get
        {
            decimal total = Quantity * (UnitPrice - (UnitPrice * (Discount / 100)));
            return total;
        }
    }        

    public override string ToString()
    {
        return StockName;
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T00:13:43+00:00Added an answer on June 3, 2026 at 12:13 am

    so, basically, what you are seeing is due to a common mis-conception about ObservableCollection. OC does NOT notify when objects that it contains are changed. It notifies when IT changes (take a look at INotifyCollectionChanged)–when items are added, removed, etc.

    what you want to do is be notified when a StockItem that is contained in the OC changes. You are going to have to do a couple of things

    1) make sure INotifyPropertyChanged (which you say you are already doing) is implemented on your StockItem
    2) customize or find an implementation of ObservableCollection that will notify when an item contained in the collection changes (here is one)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using MVVM to bind a ComboBox to an ObservableCollection in my WPF-application. However,
Basically I'm using the ItemSource property of the datagrid to bind a generic list
How do I bind the datagrid visibility using MVVM so that if there is
I have ViewModel, Which has a ObservableCollection[Employee] EmpCol , now that ViewModel is bind
I'm using the M-V-VM pattern. I have a ViewModel and an ObservableCollection of DataModels.
Has anyone managed to bind a DataNavigator and a DataGrid using the DataSource property
I'm using the WPF toolkit datagrid. I have it set to SelectionUnit=Cell and SelectionMode=Extended
I'm using the WPF DataGrid to bind to a collection of a custom class.
I am using Datagrid and SQLDataSource to bind data from just one table. In
Using a DataGrid from WPFToolkit, I bind it to a datatable. On certain scenario

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.