Binding DataGridView Objects

(We found the following URL helpful: http://www.switchonthecode.com/tutorials/csharp-tutorial-binding-a-datagridview-to-a-collection)

A DataGridView object can bind to a DataSource that must be a BindingList<BusinessObject> (from System.ComponentModel) to be able to allow two way updates. When you set the DataGridView.DataSource Property to the BindingList, the DataGridView automatically binds to the properties of the BusinessObject and sets the headings etc. You can change the bindings when you create the DataGridView Columns by setting a Columns DataPropertyName to be the BusinessObject's property name and other Column properties as needed. The steps follow:

  1. Create the BusinessObject with properties you expect to see in the DataGridView, not necessarily what you want in the database you will eventually save the object to. Make sure that you have implemented the INotifyPropertyChanged interface (required to allow two way updates).

  2. Create the DataGridView with appropriate DataPropertyName values.

  3. Set the DataGridView.DataSource = BindingList<BusinessObject>

  4. In your (object relational mapping) ORM code load the database into the BusinessObjects and add them to the BindingList<BusinessObject>

The code appears as follows:

Step 1: To see an example of a business Object see ParticleShapeAsc.

Step 2: The DataGridView fragment example appears as follows:

            // 
            // cb3C1ParticleDescription
            // 
            this.cb3C1ParticleDescription.DataPropertyName = "ParticleShapeID";
            this.cb3C1ParticleDescription.HeaderText = "Particle Type";
            this.cb3C1ParticleDescription.Name = "cb3C1ParticleDescription";
            this.cb3C1ParticleDescription.Resizable = System.Windows.Forms.DataGridViewTriState.True;
            this.cb3C1ParticleDescription.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
            // 
            // tb3C1Percent
            // 
            this.tb3C1Percent.DataPropertyName = "Percent";
            this.tb3C1Percent.HeaderText = "Percent";
            this.tb3C1Percent.Name = "tb3C1Percent";

Step 3: The Binding takes place as follows:

            edc.dg3CParticleShape.DataSource = ParticleShapes;

Above "edc" is the earth description control containing a DataGridView dg3CParticleShape.

Step 4: shows the process of reading the data from a DataTable and creating the ParticleShapeAsc object (Which does have the ability to save itself back to the database).

            //load lists of appropriate objects for each association table to be used in the datagrid databindings.
            ArchDataset.ARCH_Asc_EarthLocusC_ParticleShapeRow[] rows = (ArchDataset.ARCH_Asc_EarthLocusC_ParticleShapeRow[])ds.ARCH_Asc_EarthLocusC_ParticleShape.Select("LocusIdentity = " + parentLocus.locusIdentity + " AND SupplementID = " + this.supplementID);
            foreach (ArchDataset.ARCH_Asc_EarthLocusC_ParticleShapeRow r in rows)
            {
                //Load 'em up into objects!
                ParticleShapes.Add(
                    new ParticleShapeAsc(r, 
                        ds.ARCH_Asc_EarthLocusC_ParticleShape, 
                        tam.ARCH_Asc_EarthLocusC_ParticleShapeTableAdapter));
            }

DataGridView (last edited 2009-12-29 23:51:31 by b12webproxy05)