title: Best approach to bind a datagridview to database entity/ies
body: <p>Is this aproach the best that exist to databind a datagridview? </p>
  
  <p>I've seen many people with problems binding datagridviews and after a lot of work I found this as the best method. I hope it helps other people, and someone can add an improvement.</p>
  
  <p><strong>Step 1) Create the dataGridView and its columns with the editor.</strong></p>
  
  <p><strong>Step 2) Create an object that represents a row in the datagridview.</strong></p>
  
  <p>This object may have as many instances of database entities as you need. Here's an example with two objects (two columns in the datagridview)</p>
  
  <pre><code>public class ObjectToShow
  {
      MyDatabaseObject myDatabaseObject = new MyDatabaseObject();
  
      public ObjectToShow(MyDatabaseObject myDatabaseObject)
      {
          this.myDatabaseObject = myDatabaseObject;
      }
  
      public string Data1 //to asign to a datagridview column
      {
          get { return myDatabaseObject.data1; }
          set { myDatabaseObject.data1 = value; NotifyPropertyChanged("Data1")}
      }
  
      public string Data2 //to asign to another datagridview column
      {
          get { return myDatabaseObject.data2; }
          set { myDatabaseObject.data2 = value; NotifyPropertyChanged("Data2"); }
      }
  
      //This is to notify the changes made to the object directly and not from the control. This refreshes the datagridview.
      public event PropertyChangedEventHandler PropertyChanged;
      private void NotifyPropertyChanged(String propertyName)
      {
          if (PropertyChanged != null)
          {
              PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
          }
      }
  
  }
  </code></pre>
  
  <p><strong>Step 3) In the Form create a BindingList of ObjectToshow and a bindingSource as follow</strong></p>
  
  <pre><code>BindingList&lt;ObjectToshow&gt; ObjectToShow_list = new BindingList&lt;ObjectToshow&gt;();
  BindingSource bindingSource = new BindingSource();
  </code></pre>
  
  <p><strong>Step 4) Create the bindings this way</strong></p>
  
  <pre><code>//if we don't put this, each public property in ObjectToshow will generate a new column in the datagridview
  //I think it's best to create the columns from the editor.
  dataGridView1.AutoGenerateColumns = false;
  
  //database -&gt; &lt;- bindingList -&gt; &lt;- bindingSource -&gt; &lt;- datagridview &lt;- user
  bindingSource.DataSource = ObjectToShow_list;
  dataGridView1.DataSource = bindingSource;
  
  dataGridView1.Columns["Column_Data1"].DataPropertyName = "Data1";
  dataGridView1.Columns["Column_Data2"].DataPropertyName = "Data2";
  </code></pre>
  
  <p><strong>Step 5) query the database</strong></p>
  
  <pre><code>//Example bringing all the data from a database table. This should be done in a controller class.
  My_DBModel DB_context = new My_DBModel();    
  List&lt;myDatabaseObject&gt; myDatabaseObject_list = DB_context.myDatabaseObject.ToList();
  
  //Clear de previous data    
  ObjectToShow_list.Clear();
  
  //Add the queried elements to the bindingList
  foreach (myDatabaseObject item in myDatabaseObject_list)
  {
      ObjectToshow objectToshow = new ObjectToshow(item);
      ObjectToShow_list.Add(objectToshow);
  }
  </code></pre>
  
  <p><strong>Step 6) Modify the data from the bindingList or the datagridview as you want. Then DB_context.saveChanges().</strong></p>
  
  <p>To add data, add it directly to DB_context.myDatabaseObject.Add(new ...) and query the database again; If you want to add it from the datagridview I think you have to handle the event and add it to the context anyway.</p>
  
  <p>This is what I do and it works, but I'm not sure if it's the best way. Thanks in advance.</p>