Bind Objects to a DataGridView Control



Introduction: 

This article describes a simple approach to displaying object property data within a data grid view control. The example includes a test application comprised of a simple data container class and a sample application used to bind and display object data within a data grid view control. The value of the approach is to permit a developer to quickly display object data in tabular format, or to provide a convenient method for editing object data.


BindingObjects1.gif

Figure 1:  Displaying Object Data in a Grid

Getting Started:
In order to get started, unzip the attachment and load the solution into Visual Studio 2005. Examine the solution explorer and note the files contained in the project:
The contents of the solution show that it contains a single project entitled, "BindObjects". This project contains a single form class called "Form1". The Form1.cs class contains the Form1 class definition and I have added a second class to the file; that class is called, "TableData" and it is merely a dummy data container class with three properties.
A quick check of the references will reveal that only the default class libraries are contained in the project.

The Code:  The Test Data Class.

The TestData class serves no useful purpose; the intent for including this class is merely to provide a source for object data that will subsequently be bound to the data grid view control used in the form class.
The code used in the class is as follows:

public class TableData

{                 

    private string mOwner;

    private string mPet;

    private string mPetname;

                  

    public string Owner

    {

        get

        {

            return mOwner;

        }

        set

        {

            mOwner = value;

        }

    }

                  

    public string Pet

    {

        get

        {

            return mPet;

        }

        set

        {

            mPet = value;

        }

    }

                  

    public string PetName

    {

        get

        {

            return mPetname;

        }

        set

        {

            mPetname = value;

        }

    }                  

}


As you can see, the class is pretty basic. It contains three private member variables and three properties used to set and retrieve the variable content. Again, the only purpose of this class is to provide something to bind the grid to at runtime in support of this demonstration.

The Code:  Form 1 Class.


This class provides the one and only form used in the demonstration application. The form class does not include anything other than the default imports and it only contains a single control which is the grid view control. The code contained in this class is as follows:

public class Form1

{

    private void Form1_Load(System.Object sender, System.EventArgs e)

    {                            

        TableData td = new TableData();

        td.Owner = "Frank Lloyd Wright";

        td.Pet = "Cow";

        td.PetName = "Felix";

                            

        TableData td2 = new TableData();

        td2.Owner = "George Washington";

        td2.Pet = "Dog";

        td2.PetName = "Monty";

                            

        TableData td3 = new TableData();

        td3.Owner = "Carl Sandburg";

        td3.Pet = "Frog";

        td3.PetName = "County";

                            

        TableData[] arr = new TableData[4];

        arr[0] = td;

        arr[1] = td2;

        arr[2] = td3;

                            

        DataGridView1.DataSource = arr;

                            

        //autoresize each column

        DataGridView1.AutoResizeColumn(0);

        DataGridView1.AutoResizeColumn(1);

        DataGridView1.AutoResizeColumn(2);                            

    }                  

}

 

The form load event handler contains all of the code used to bind the object data to the data grid view. In this instance, three objects are created from the TableData class and each of these three objects is populated with data (objects td, td2, and td3). To bind all three of the objects to the data grid view control, each of the three objects is added to an array of TableData.  Once the array has been created, each of the three objects is added to the array and then the data grid view control's data source property is aimed at the array. The last bit just adjusts the column widths to display all of the content at runtime based upon the maximum width of any given column. Once executed, the application's form will display all of the entered table data within the data grid view control.

Summary.


The purpose of the example is to demonstrate an approach to binding a collection of objects to a data grid view control.  Additional code could be added to make the contents of the grid editable at runtime. The only purpose of the example was to illustrate the potential for displaying object data in a grid as a means for reviewing and possibly editing such content at runtime.
NOTE: THIS ARTICLE IS CONVERTED FROM VB.NET TO C# USING A CONVERSION TOOL. ORIGINAL ARTICLE CAN BE FOUND ON VB.NET Heaven (http://www.vbdotnetheaven.com/).