title: WPF: Databinding to a DataGrid
body: <p>This is my first post in this forum, though I am a long-time lurker. I have started learning WPF for about a couple of months now, and I am trying to create an application just for training purposes.</p>
  
  <p>I have a backend database which I have added to my application using EF6 ORM. In my application, I have a `ComboBox which needs to be populated by a column in a table of the database. That I can do using binding to a list.</p>
  
  <p>The part I am having trouble with is the <code>DataGrid</code>. The columns of the <code>DataGrid</code> needs to be populated according to the <code>Item</code> chosen in the <code>ComboBox</code>.</p>
  
  <p>My database:</p>
  
  <p><img src="https://i.stack.imgur.com/oGYkw.png" alt="Here is my database structure"></p>
  
  <p>As you can see, the school has several departments, and each of those department has a HOD and a student strength.</p>
  
  <p>My application:</p>
  
  <p><img src="https://i.stack.imgur.com/o0AAW.png" alt="Here is the snapshot of the app"></p>
  
  <p>The <code>ComboBox</code> will be populated with school names. The <code>DataGrid</code> will be populated once the schoolname is selected. The <code>DataGrid</code> will have each row for each department available for the school. So I need to bind the corresponding columns with the departments of the corresponding schools. That much I get. However, then I want to save the user-entered comments in the Feedback <code>TextBox</code>.</p>
  
  <p>I cannot understand how to create a class so that I can bind the <code>DataGrid</code> to the object of it. Is it possible to bind the <code>DataGrid</code> to an object and then bind the columns separately to another object?</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>Apart from the entities created from the database, I have two classes:</p>
  
  <pre><code>class Feedback : INotifyPropertyChanged
  {
      public event PropertyChangedEventHandler PropertyChanged;
  
      private string _school;
  
      public string School
      {
          get
          {
              return _school;
          }
  
          set
          {
              _school = value;
              OnPropertyChanged("School");
          }
      }
  
      private ObservableCollection&lt;FeedbackLine&gt; _feedbackLines;
  
      public ObservableCollection&lt;FeedbackLine&gt; FeedbackLines
      {
          get
          {
              return _feedbackLines;
          }
  
          set
          {
              _feedbackLines = value;
              OnPropertyChanged("FeedbackLines");
          }
      }
  
      public Feedback(string school)
      {
         //Insert some Linq Query to populate the FeedbackLines
         //something like
         //var FeedbackLines = Context.Schools.Where(c =&gt; c.SchoolName == school)
         //                                   .Select(c =&gt; new {Department = c.AvailableDepts.Dept, etc etc}.ToList();
         //but then what?
      }
  
      private void OnPropertyChanged(string v)
      {
          PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(v));
      }
  }
  </code></pre>
  
  <p>This is supposed to be bound to the datagrid. And the FeedbackLine is:</p>
  
  <pre><code>public class FeedbackLine: INotifyPropertyChanged
  {
      private string _dept;
  
      public string Department
      {
          get { return _dept; }
          set { _dept = value;
              OnPropertyChanged("Department");
          }
      }
  
      private string _HOD;
  
      public string HOD
      {
          get { return _HOD; }
          set { _HOD = value;
              OnPropertyChanged("HOD");
          }
      }
  
      private int _strength;
  
      public int Strength
      {
          get { return _strength; }
          set { _strength = value;
              OnPropertyChanged("Strength");
          }
      }
  
      private bool _isSelected;
  
      public bool Selected
      {
          get { return _isSelected; }
          set { _isSelected = value;
              OnPropertyChanged("Selected");
          }
      }
  
      private string _comment;
  
      public string Comment
      {
          get { return _comment; }
          set { _comment = value;
              OnPropertyChanged("Comment");
          }
      }
  
      private void OnPropertyChanged(string v)
      {
          PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(v));
      }
  
  
  
      public event PropertyChangedEventHandler PropertyChanged;
  }
  </code></pre>
  
  <p>I haven't had much headway with the ViewModel. Problem is, I am not very good with LINQ. And there are too many classes and objects and I have no idea which one to bind with which. The only vague idea that I can get is that I have to use LINQ to query the database using the selected <code>School</code>, and then populate the <code>FeedbackLines</code> using that.</p>
  
  <p><strong>Edit 2:</strong>
  For anyone who's interested, here's my model diagram in WPF:
  <a href="https://i.stack.imgur.com/UJw2p.png" rel="nofollow noreferrer">Model Diagram</a></p>
  
  <p><strong>Edit 3:</strong>
  I think I am confused about ViewModel. The data that will be displayed on the screen is not necessarily the data to be saved. For example, I don't want to save the unselected rows. My <code>Feedback</code> class tries to display the data as well as save it. And therein lies the problem. Can't a <code>DataGrid</code> be bound to an object, while its columns be bound to other objects? For example, if I choose to use a <code>Combobox</code> for Department. Then I need to use <code>ItemsSource</code> for displaying items, but need to save the <code>SelectedItem</code> only. I can't find a way to separate these two concerns.</p>