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 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) {CreateRrayList (); 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 thing 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;}} // 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 NotALoW 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 (transovet> 0) {cm.position = REMOVEAT - 1;} // remove current entry arraylist1.removeat (transovet); if (cm! = null) {cm.refresh ();}} // select the entire row when User Click ON A Cell in The Row Private Void DataGrid1_mouseup (Object Sender, System.Windows E) {System.drawing.Point Pt = New Point (EX, EY);