BindingRelatedObjectsToDataGridView

When using the EntityFramework4 You can bind a DataGridView to related entities (linked entities) by using the DataBindingComplete event. For example (From Lerman, J. "Programming Entity Framework" O'Reilly. pp. 199-200):

   1         private void reservationsDataGridView_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
   2         {
   3             DataGridView gridView = (DataGridView)sender;
   4             foreach (DataGridViewRow row in gridView.Rows)
   5             {
   6                 BAGA.Reservation reservation = (BAGA.Reservation)(row.DataBoundItem);
   7                 BAGA.Trip trip = reservation.Trip;
   8                 row.Cells[tripStartColumn.Index].Value = trip.StartDate.ToShortDateString();
   9                 row.Cells[tripEndColumn.Index].Value = trip.EndDate.ToShortDateString();
  10                 row.Cells[destinationColumn.Index].Value = trip.Destination.DestinationName;
  11             }
  12         }

NOTE: In a more highly architected application, you would likely be using pattern that would not force you to perform this type of logic. E.g. Your application may have a presentation objects layer? Although I fail to see the advantages of this pattern for many applications. Might it be better to either design your database for the best mix of presentation and normalization (not always at odds interestingly) or to develop your object first. Just a thought.

ProgrammingLinks/BindingRelatedObjectsToDataGridView (last edited 2011-02-24 14:46:43 by 24-151-193-255)