Generics : An easy way to bind the data in DataGridView

Generics : An easy way to bind the data in DataGridView

Rate This
  • Comments 22

Applied to: Visual Studio 2005 [C#]

 

Yes this is Generics. The concept which helps us to create collection easily and elegantly. Two steps to create the list is demonstrated here.

 

Step 1:

 

You have one object say for list of products which contains three properties Name, Quantity and Price.

 

using System;

using System.Collections.Generic;

using System.Text;

 

namespace MSDN_Generics

{

    class Product

    {

    

        private int _Quantity;

 

        public int Quantity

        {

            get { return _Quantity; }

            set { _Quantity = value; }

        }

 

        private int _Price;

 

        public int Price

        {

            get { return _Price; }

            set { _Price = value; }

        }

 

        private string _Name;

 

        public string Name

        {

            get { return _Name; }

            set { _Name = value; }

        }

 

        /// <summary>

        /// Constructor to initialize the class

        /// </summary>

        /// <param name="sName"></param>

        /// <param name="iQty"></param>

        /// <param name="iPrice"></param>

        public Product(string sName, int iQty, int iPrice)

        {

            _Name = sName;

            _Quantity = iQty;

            _Price = iPrice;

        }

     

    }

}

 

 

Step 2: Create a List of this class to use it for DataGridView’s datasource.

 

            List<Product> myProds = new List<Product>();

 

            myProds.Add(new Product("Prod 1", 1, 1));

            myProds.Add(new Product("Prod 2", 2, 2));

            myProds.Add(new Product("Prod 3", 3, 3));

            myProds.Add(new Product("Prod 4", 4, 4));

            myProds.Add(new Product("Prod 5", 5, 5));

            myProds.Add(new Product("Prod 6", 6, 6));

            myProds.Add(new Product("Prod 7", 7, 7));

 

            dataGridView1.DataSource = myProds;

 

You need a namespace reference to use this Generic listing System.Collections.Generic

 

Attachment: MSDN_Generics.zip
Leave a Comment
  • Please add 5 and 5 and type the answer here:
  • Post


  • I know generigs is much deeper in .NET but this was so clear that I had
    to show hou you could use...
  • Good post. I also found out somewhat accidentally that if you add only a single object into a generic list, you can bind that list to a detailsview, and the detailsview will automatically list out all public properties and their values by default.

    I don't know if this is useful to anyone (or even a good idea), but I thought I'd toss that out there.

    Roger
  • Is it also possible to bind a treeview to a generic list?
  • RogerH,

    You are right. It is very usefull especially when we have county and state selection option.

    WRIJU
  • How to do editing and update save in database directly form DataGridView in VB.NET pl. tel me in very simple form
    let a two fields id and name in a db and i want to edit and add update data with DataGridView
    in VB.NET
  • How to do editing and update save in database directly form DataGridView in VB.NET pl. tel me in very simple form
    let a two fields id and name in a db and i want to edit and add update data with DataGridView
    in VB.NET
  • Hi...
    anyone can say, howz a control databind with generic List
  • We are trying to reach up to collegeous student and work for them. We careate to develop or group teh knowledge base for them. Software Section is also available in our site. We are for u.

  • How about updating changes made in the datagrid to the underlying datasource(object List)

    OR

    Reloading the datagrid to show changes made to properties of the object list

  • Object model update can be done through DataContext in LINQ to SQL.

    Please visit http://blogs.msdn.com/wriju/archive/2007/07/17/linq-to-sql-update-data-through-object-model.aspx

    Wriju

  • dear sir,

    i did what u say , but i cant edit the data through the datagridview.

    when i edit any cell then exit from it , it returns to its original value.

    if u know the reason please reply to me.

    aramadan

    vip174@yahoo.com

    thx

  • Please update the DataTable which is the source for DataGrid

  • hi friends,

    i want some codings for o save the datagrid values in database.

    and also another one is how to convert the foxpro,MsAccess, Excel to SQL 2000 i.e (.dbf, .mdb, .exl to .sql).

    please as soon as possible.

    Thanking u,

    sridhar

  • dear sir,

       how to add my datas retrived form a table into the Grid view please help me..

  • dear sir,

       how to add my datas retrived form a table into the Grid view please help me..

Page 1 of 2 (22 items) 12