0

I have this Master-Details windows form that I've been trying to finish for some time and the next step is to change the value in the data grid view each time the values of these two columns change: cantitate (quantity) and id_produs (id_product).

Product is a combobox binded to a table of Products in SQL. enter image description here

I want when I change the quantity or the product in the combobox, the value on the corresponding row to change according to the formula: valoare = pret_unitar * cantitate. Is it possible to make the value autocomplete in real time after I change the product or quantity (and when I add a new product and quantity)?

Just in case you need to know, the code behind the add and change buttons is:

            this.Validate();
            this.tbl_IntrariBindingSource.EndEdit(); //validate master table
            this.tbl_intrari_detaliuBindingSource.EndEdit(); //validate detail table (the table I mentioned here)
            this.tableAdapterManager.UpdateAll(this.myDatabaseDataSet);
Felix
  • 1,066
  • 1
  • 5
  • 22
Maria
  • 13
  • 4
  • which UI framework are you using? (WPF / WinForms) – Felix Jun 08 '22 at 06:01
  • I'm using WinForms – Maria Jun 08 '22 at 06:58
  • You may want to look into implementing `INotifyPropertyChanged` into your class that holds the values you are updating. Then when you update the property, you can have actions also triggered such as updating visual fields. See: https://stackoverflow.com/questions/1315621/implementing-inotifypropertychanged-does-a-better-way-exist And : https://learn.microsoft.com/en-us/dotnet/desktop/winforms/how-to-implement-the-inotifypropertychanged-interface?view=netframeworkdesktop-4.8 – Ibrennan208 Jun 08 '22 at 07:21
  • You could add a DataColumn to the DataTable in the TableAdapter and create an [Expression](https://learn.microsoft.com/en-us/dotnet/framework/data/adonet/dataset-datatable-dataview/creating-expression-columns) – Karen Payne Jun 08 '22 at 08:57

1 Answers1

0

Add this class to your project:

using System;
using System.ComponentModel;
using System.Runtime.CompilerServices;

namespace misc
{
    public abstract class NotifyPropertyChangeBase : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        public void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }
}

In your class (or classes, fit it to your needs) do this:

(...)

    private double quantity;
    private double prec_unit;

    public double Quantity
    {
        get { return quantity; }
        set
        {
            this.quantity= value;
            NotifyPropertyChanged();
        }
    }

    public double Prec_unit
    {
        get { return prec_unit; }
        set
        {
            this.prec_unit= value;
            NotifyPropertyChanged();
        }
    }

   public double Valore
    {
        get 
        {
            return this.quantity * this.prec_unit;
            NotifyPropertyChanged();
        }
    }

(...)

Hope it helps.

  • Thank you. This code needs adjustments as it gives me an error, but it's a good starting point. I'm going to read more about the INotifyPropertyChanged since I will need it in further projects :) – Maria Jun 15 '22 at 06:39
  • ...my bad... Replace "NotifyPropertyChangeBase " for "NotifyPropertyChanged" on the class... – Ricardo Rodrigues Jun 15 '22 at 08:49