Overview
Here is a technique for binding an arraylist of objects where the objects contain public property that can appear as columns in the datagrid. In this example, the object contains 2 public doubles, one named "RandomValue" and the other named "SqrtValue". To bind this arraylist to a datagrid, add a custom tablestyle that has a MappingName of "ArrayList", and then use the property names as the MappingName for each column.
Example
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace Akadia.DataGridArrayList
{
public class DataGridArrayList : System.Windows.Forms.Form
{
private System.Windows.Forms.DataGrid dataGrid1;
private ArrayList arrayList1;
private System.Windows.Forms.Button buttonAddNew;
private System.Windows.Forms.Button buttonDeleteCurrent;
private System.ComponentModel.Container components = null;
public DataGridArrayList()
{
InitializeComponent();
}
...
...
[STAThread]
static void Main()
{
Application.Run(new DataGridArrayList());
}
// Create and Bind ArrayList to the Datagrid when loading
private void DataGridArrayList_Load(object sender, System.EventArgs e)
{
CreateArrayList();
BindArrayListToGrid();
}
// Create a custom tablestyle and add two columnstyles
private void BindArrayListToGrid()
{
dataGrid1.DataSource = arrayList1;
DataGridTableStyle ts = new DataGridTableStyle();
ts.MappingName = "ArrayList";
int colwidth = (dataGrid1.ClientSize.Width
- ts.RowHeaderWidth
- SystemInformation.VerticalScrollBarWidth - 5) / 2;
// Create a column for the "RandomValue" property
// defined in the RandomNumber Class
DataGridTextBoxColumn cs = new DataGridTextBoxColumn();
cs.MappingName = "RandomValue"; // Public property name
cs.HeaderText = "Random Number";
cs.Format = "f4";
cs.Width = colwidth;
ts.GridColumnStyles.Add(cs);
// Create a column for the "SqrtValue" property
// defined in the RandomNumber Class
cs = new DataGridTextBoxColumn();
cs.MappingName = "SqrtValue"; // Public property name
cs.HeaderText = "Square Root";
cs.Format = "f4";
cs.Width = colwidth;
ts.GridColumnStyles.Add(cs);
// Add the custom tablestyle to the DataGrid
dataGrid1.TableStyles.Clear();
dataGrid1.TableStyles.Add(ts);
}
private void CreateArrayList()
{
arrayList1 = new ArrayList();
// Add some random Numbers
Random r = new Random();
for (int i = 0; i < 20; ++i)
{
arrayList1.Add(new RandomNumber(r.NextDouble()));
}
}
// Create a struct or class that defines what you want in each row
// the different columns in the row must be public properties
public class RandomNumber
{
private double num;
// Constructor
public RandomNumber(double d)
{
num = d;
}
// Public Property "RandomValue" used
// as Column in the DataGrid
public double RandomValue
{
get{ return num; }
set{ num = value;}
}
// Public Property "SqrtValue" used
// as Column in the DataGrid
public double SqrtValue
{
get {return Math.Sqrt(this.num);}
}
}
// Add a new random Number to the ArrayList and
// then refresh the DataGrid
private void buttonAddNew_Click(object sender, System.EventArgs e)
{
Random r = new Random();
arrayList1.Add(new RandomNumber(r.NextDouble()));
// Refresh the Datagrid with the new random number
CurrencyManager cm =
(CurrencyManager) this.dataGrid1.BindingContext[arrayList1];
if (cm != null)
{
cm.Refresh();
}
}
// Delete current Entry from the ArrayList.
private void buttonDeleteCurrent_Click(object sender, System.EventArgs e)
{
// Get the current position due to not allow empty datagrid.
CurrencyManager cm =
(CurrencyManager) this.BindingContext[dataGrid1.DataSource];
if (cm.Count <= 1)
{
return; // Do not allow an empty DataGrid
}
// Place cursor on position up
int removeAt = cm.Position;
if (removeAt > 0)
{
cm.Position = removeAt - 1;
}
// Remove current Entry
arrayList1.RemoveAt(removeAt);
if (cm != null)
{
cm.Refresh();
}
}
// Select the entire row when the user clicks on a cell in the row
private void dataGrid1_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
{
System.Drawing.Point pt = new Point(e.X, e.Y);
DataGrid.HitTestInfo hti = dataGrid1.HitTest(pt);
if (hti.Type == DataGrid.HitTestType.Cell)
{
dataGrid1.CurrentCell = new DataGridCell(hti.Row, hti.Column);
dataGrid1.Select(hti.Row);
}
}
}
}