Binding an ArrayList with DataGrid
This article explain about Binding an ArrayList with DataGrid with code snippet for Arraylist .
Binding an ArrayList with DataGrid. To bind the arraylist to a datagrid, the arraylist shold contain object which will contain the data.
To bind the data of Arraylist, which is stored in the of objects; all the data members of the class must be implemented as properties. then it is possible to bind the list of objects stored in arraylist can bind to the datagrid.
Step 1: create a class for the objects which will be stored in arraylist
public class Student
{
private string firstName;
private string lastName;
private int age;
public Student(string firstName, string lastName, int age)
{
this.firstName = firstName;
this.lastName = lastName;
this.age = age;
}
public string FirstName
{
get { return this.firstName; }
}
public string LastName
{
get { return this.lastName; }
}
public int Age
{
get { return this.age; }
}
}
Step 2: create list of objects which will be bind to the datagrid
private ArrayList GetList()
{
ArrayList list = new ArrayList();
list.Add(new Student("SanjY", "Bollina", 27)) ;
list.Add(new Student("Sanjeev", "Bollina", 27)) ;
return list;
}
Step 3: Bind the datagrid
private ArrayList GetList()
{
ArrayList list = GetList();
dataGrid1.DataSource = list;
}
You did a great job!!! It was really very helpful.
Thanks,
Kiran
----------------------
http://india-jobs-opening.blogspot.com/