I
I
Ilya2014-02-20 05:34:06
WPF
Ilya, 2014-02-20 05:34:06

How to link a list in a table to a linked table?

Hello.
The task arose to write an application for a typical product accounting. Chose MSSQL, WPF, Entity Framework. In this case, I have a table that is displayed in the DataGrid and other tables associated with it, like reference tables ( For example : Table "Metal category", "Name", etc.).
The problem is that I don't know how to make the table have a ComboBox element that is filled with a reference book, and when an element is selected, the adjacent column would be filled with the mass of this element taken from this table.
Example:
482ad1fe9f74.png
Here is the source of the models:

//таблица Stelug - справочник стелюг
public partial class Stelug
    {
        public Stelug()
        {
            this.plavkas = new HashSet<plavka>();
        }
    
        public int id { get; set; }
        public int nStelug { get; set; }
        public double mass { get; set; }
    
        public virtual ICollection<plavka> plavkas { get; set; }
    }

//таблицы Plavka - основная таблица где вся информация
public partial class plavka
    {
        public plavka()
        {
            this.chemicals = new HashSet<chemical>();
            this.paketPlavkas = new HashSet<paketPlavka>();
        }
    
        public System.Guid id { get; set; }
        public int plavka1 { get; set; }
        public System.DateTime dataPrig { get; set; }
        public int catMetal { get; set; }  //это связаное поле с таблицой catMetal
        public Nullable<int> smena { get; set; }
        public Nullable<int> masterFIO { get; set; }  //это связаное поле с таблицой FioDict
        public Nullable<int> nSteluga { get; set; } //это связаное поле с таблицой Stelug
        public Nullable<double> mBrutto { get; set; }
        public Nullable<double> mNetto { get; set; }     
        public Nullable<int> nKonveera { get; set; }
        public Nullable<int> otkFIO { get; set; }
        public bool status { get; set; }
        public bool brak { get; set; }
        public Nullable<double> mBrak { get; set; }
        public string comments { get; set; }
    
        public virtual catMetal catMetal1 { get; set; }      
        public virtual FioDict FioDict { get; set; }      
        public virtual ICollection<paketPlavka> paketPlavkas { get; set; }
        public virtual Stelug Stelug { get; set; }
    }

Well, then I made a class where I described the connection with EF:
public class MainViewModel : INotifyPropertyChanged, IDisposable
    {
        /// <summary>
        /// Наш контекст данных
        /// </summary>
        private MagnesiumEntities _dataContext;
 
        /// <summary>
        /// таблица "Плавок"
        /// </summary>
        public ObservableCollection<plavka> Plavkas { get; set; }

        public IEnumerable<string> CatMetals { get; private set; }
        public IEnumerable<string> FioDict { get; private set; }
        public IEnumerable<Stelug> Stelug { get; private set; }

        public MainViewModel()
        {
            _dataContext = new MagnesiumEntities();
            Plavkas = new ObservableCollection<plavka>(_dataContext.plavkas);

            CatMetals = (from db in _dataContext.catMetals select db.category).ToList();
            FioDict = (from db in _dataContext.FioDicts select db.FIO).ToList();
            Stelug = (from db in _dataContext.Stelugs select db).ToList();

            Save = new ActionCommand(SaveChanges) { IsExecutable = true };
        }

        private void SaveChanges()
        {
            _dataContext.SaveChanges();
        }
 
        #region Команды

        /// <summary>
        /// Команда для сохранения
        /// </summary>
        public ActionCommand Save { get; set; }

        #endregion
 
        #region INotifyPropertyChanged 
        
        public event PropertyChangedEventHandler PropertyChanged;        
        private void RaisePropertyChanged(string propertyName)
        {
            if (PropertyChanged == null)
            {
                return;
            }
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
 
        #endregion
}

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question