$99 VS $1000+. UI Control for ASP.NET AJAX(contains chart and gridview).
DataGridView WinForms Control
Powerful GridView C#.NET control supports data binding to various data sources
Home > WinForms UI Controls > User Manual > Binding to Array and ArrayList in C#

Bind C# DataGridView to Array and ArrayList

When bind the DataGridView component to Array and Arraylist, which contains supported data types available, we can easily assign the array to the DataSource property of the grid view in C# Windows Forms applications. In this article, we are going through the process of binding GridView C#.NET component to a generic list, an Array and ArrayList of custom business objects. While binding GridView to Array and ArrayList, we need to use the BindingList as the collections, which will automatically update any changes to the GridView control that has been bound with.

GridView Data Binding to Generic List

Since the BindingList is the preferred collection to synchronize any changes to the GridView C#.NET component automatically. We are going to create an ArrayList of generic objects which will be initialized by 5 values, and then assigned as a DataSource to the Kettic GridView .NET Control. The following are the examples that show how to create a simple C# array class and then binding the GridView control to the C# array class.

public class ValueType<T>
{
T item;

public ValueType() { }

public ValueType(T item)
{
this.item = item;
}

public T ItemProperty
{
get { return this.item; }
set { this.item = value; }
}
}

ArrayList list = new ArrayList();
for (int i = 0; i < 5; i++)
{
list.Add(new ValueType<string>("string " + (i + 1).ToString()));
}

this.ketticGridView1.DataSource = list;

GridView Data Binding to Array of Objects

The Kettic GridView component is capable of binding to Arrays of objects that includes support data types. This is achieved by assigning the array to the DataSource property of the DataGriView control. In the example as following, we will define a MyObject C# class which will contains the integer and string property and create an array of MyObject in C# to initialize the array with objects. And then we will assign the array to the DataSource property.
C# code for create simple object class for GridView data binding

public class MyObject
{
public MyObject(int myInt, string myString)
{
_myInt = myInt;
_myString = myString;
}
private int _myInt;
public int MyInt
{
get { return _myInt; }
set { _myInt = value; }
}
private string _myString;
public string MyString
{
get { return _myString; }
set { _myString = value; }
}
}
C# code for binding GridView component to object array in Windows Forms application

MyObject[] myArray = new MyObject[2] {new MyObject(1, "object one"), new MyObject(2, "object two") };
ketticGridView1.DataSource = myArray;

GridView Data Binding to ArrayList

The following are the C# code snippet that shows how to bind the GridView C#.NET control to an ArrayList.

ArrayList arrayList = new System.Collections.ArrayList();
arrayList.Add(new MyObject(1, "Object A") );
arrayList.Add(new MyObject(2, "Object B") );
arrayList.Add(new MyObject(3, "Object C") );
ketticGridView1.DataSource = arrayList;
UI Controlsfor Windows Forms
.NET WinForms UI Overview.NET WinForms UI Features.NET WinForms UI GuideC# WinForms UI DesignVB.NET WinForms UI Design
WinForms UI Controls