Loading ...

C# DataGridView control with custom collection in Windows Forms | CodeAsp.Net

C# DataGridView control with custom collection in Windows Forms

(64280)
0
/5
Avg: 0/5: (0 votes)
Published: 3/22/2009 by Vivek Thakur

Introduction

Windows DataGridView control in .NET 2.0 and above supercedes the old DataGrid control. The DataGridView control provides many basic and advanced features that are missing in the DataGrid control. Also, it is easier to to extend and customize the DataGridView control than the DataGrid control.

Implementation Code

Here is a simple method on how to implement and use a basic DataGridView control in your windows applications. First create a simple Windows Forms Application in your VS 2008 IDE. Then on the default form (MyForm.cs), drag and drop a datagridview control and name it as dgridCustomers. We will be binding this grid with a Customer collection (custom generic collection).

To start with,assume that you have a customer class as:

public class Customer

{

    private int _customerID;
    private string _name;
    private DateTime _dob;  //date of birth
    private string _phoneNumber;

    public int CustomerID
    {
        get { return _customerID; }
        set { _customerID = value; }
     }

     public string Name
    {
        get { return _name; }
        set { _name= value; }
     }

     public DateTime DOB
    {
        get { return _dob; }
        set { _name= dob; }
     }

     public string PhoneNumber
    {
        get { return _phoneNumber; }
        set { _phoneNumber= value; }
     }

    //methods go here
}


Now create a method to set the number of columns and layout of the grid as follows (in the MyForm.cs class):

   private void SetGridLayout()
        {

           //clear any previously set columns
            dgridExample.Columns.Clear();           

            DataGridViewTextBoxColumn dgridColID = new DataGridViewTextBoxColumn();
            dgridColID .HeaderText = "ID";
            dgridColID .Name = "ColCustomerID";
            dgridColID .Width = 20;
            dgridColID .DataPropertyName = "CustomerID";
            dgridCustomers.Columns.Add(dgridColID );

            DataGridViewTextBoxColumn dgridColName = new DataGridViewTextBoxColumn();
            dgridColName .HeaderText = "Customer Name";
            dgridColName .Name = "ColName";
            dgridColName .Width = 200;
            dgridColName .DataPropertyName = "FullName";
            dgridCustomers.Columns.Add(dgridColName );

            DataGridViewTextBoxColumn dgridColDate = new DataGridViewTextBoxColumn();
            dgridColDate .HeaderText = "Date of Birth";
            dgridColDate.Name = "ColDate";
            dgridColDate.Width = 70;
            dgridColDate.DataPropertyName = "DateOfBirth";
            dgridCustomers.Columns.Add(dgridColDate);


            DataGridViewTextBoxColumn dgridColPhone = new DataGridViewTextBoxColumn();
            dgridColPhone .HeaderText = "Source Directory";
            dgridColPhone .Name = "ArchiveDirectory";
            dgridColPhone .Width = 100;
            dgridColPhone .DataPropertyName = "PhoneNumber";
            dgridCustomers.Columns.Add(dgridColPhone );
          
        }


Next we will bind this datagridview control with a list of Customer objects, as follows:


   private void BindCustomers()
        {

 

            //assume that we have a CustomerCollection class which is returning
           // a strongly typed list of all customers (a collection of Customer objects)
            CustomerCollection coll = new CustomerCollection()
            Collection<Customer> customers = coll.FindCustomers();
            if (customers.Count > 0)
            {           

                dgridCustomers.DataSource= customers;

            }         
        }

The above code simply binds our datagrid with the list of the objects returned. Now we will put the following code in the form's constructor so that we can set the autogeneratecolumns property to false and call the bind method.

public MyForm()
        {
            InitializeComponent();
           
            dgridCustomers.AutoGenerateColumns = false;           
            SetGridLayout();
           
            BindCustomers();
        }


We have set the AutoGenerateColumns property to false because we do not want the datagridview to add columns by itself based on the list of properties in the collection. Since we have already set the columns we want to show in the grid, we will disable the automatic columns generation.

So we have seen that using datagridview is really simple and easy, with the flexiblity of having our own custom layouts.

 

Comments (4)

Gee thank you for that lovely piece of insight.... nobody cares
6/19/2009
 · 
 
by
rowifi
rob field said:
' //assume that we have a CustomerCollection class ..... returning a strongly typed list ... ' It would be infinitely useful as a novice to see how we do this in this context .. as I don't know how!
8/31/2009
 · 
 
by
rowifi
rob field said:
' //assume that we have a CustomerCollection class ... returning a strongly typed list .. ' As a novice - it would be infinitely useful to see how this is done here. Thanks!
8/31/2009
 · 
 
by
 said:
Could you please let me know If i want to add any usercontrol in the column , how can i do that?
2/28/2010
 · 
 
by

Top articles

Quick Vote

What kind of email newsletter would you prefer to receive from CodeAsp.Net?