bind arraylist with gridview control in asp net application

In this article, we will create an ArrayList of Item objects and bind it to a GridView control using DataSource property.

Steps:



We will create a class named Item as shown below. In the Page_Load method, we will create an ArrayList and add items to the arraylist using Add method. Now we will bind the arrayList using GridView.DataSource property.

public class Item
 {
     private string strCode;
     private string strName;

     public Item(string code, string name)  
     {
        this.strCode = code;
        this.strName = name;            
     }      
       
 	public string Code     
    {        
      get 
      {   return this.strCode;    }      
    }
    public string Name
    {
       get
       {    return this.strName; }
       }
    }
}

protected void Page_Load(object sender, EventArgs e)
 {
    ArrayList list = new ArrayList();
    list.Add(new Item("I001", "Item1"));
    list.Add(new Item("I002", "Item2"));
    list.Add(new Item("I003", "Item3"));
    list.Add(new Item("I004", "Item4"));
           
    GridView1.DataSource = list;
    GridView1.DataBind();
 }