Generics : An easy way to bind the data in DataGridView
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
Comments
Anonymous
May 05, 2006
I know generigs is much deeper in .NET but this was so clear that I had
to show hou you could use...Anonymous
May 05, 2006
The comment has been removedAnonymous
May 08, 2006
Is it also possible to bind a treeview to a generic list?Anonymous
May 09, 2006
RogerH,
You are right. It is very usefull especially when we have county and state selection option.
WRIJUAnonymous
August 16, 2006
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.NETAnonymous
August 16, 2006
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.NETAnonymous
September 20, 2006
Hi...
anyone can say, howz a control databind with generic ListAnonymous
December 29, 2006
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.Anonymous
July 25, 2007
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 listAnonymous
July 25, 2007
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 WrijuAnonymous
September 11, 2007
The comment has been removedAnonymous
October 01, 2007
Please update the DataTable which is the source for DataGridAnonymous
January 04, 2008
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, sridharAnonymous
March 29, 2008
dear sir, how to add my datas retrived form a table into the Grid view please help me..Anonymous
March 29, 2008
dear sir, how to add my datas retrived form a table into the Grid view please help me..Anonymous
May 03, 2008
Dear sir, my problem is that after binding the grid with generic list the grid is not adding new rows. i want to add new rows in it although AllowUserToAddRows property is set to trueAnonymous
December 16, 2008
The comment has been removedAnonymous
July 10, 2009
sir, Sorry, i don't know english .I have a problem for databinding.i use datagdridview in c#(asp.net) I give dtGridView.databind();but it show an error.Plese help me sir,how can i use databind() ThanksAnonymous
October 16, 2009
I just wanted to make a note of something I discovered: It seems like the object representing an item in the list must use properties with get/set accessors in order for this to work. I was doing exactly the same, but instead of using properties I was using public variables... it didn't work.Anonymous
October 19, 2009
@Greg, Right one.Anonymous
September 29, 2010
when I try to add any other products after List<Product> myProds = new List<Product>(); myProds.Add(new Product("Prod 1", 1, 1)); myProds.Add(new Product("Prod 2", 2, 2)); dataGridView1.DataSource = myProds; myProds.Add(new Product("Prod 5", 1, 1)); myProds.Add(new Product("Prod 6", 2, 2)); DataGridView do not show this products. why?????????Anonymous
September 29, 2010
The comment has been removed