How to Bind an ArrayList to a DataGrid
2006-03-17 14:06
344 查看
How to Bind an ArrayList to a DataGridMore Information on installing the .Net Framework click here.Download full Visual Studio C# .NET Examples from this Article. |
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); } } } } |
相关文章推荐
- How to Bind an ArrayList to a DataGrid
- How to Bind an ArrayList to a DataGrid -- 解决ArrayList刷新问题
- Java – 怎样重新将 ArrayList 重新洗牌(How to shuffle an ArrayList)
- Jdeveloper: How to bind ArrayList to a Table for data controls
- How to Confirm a Delete in an ASP.NET Datagrid...
- How to implement an ArrayList structure in Java - Tutorial
- How do I bind the values of an enum to a ComboBox?
- How to Bind Silverlight DataGrid From IEnumerable of IDictionary by Transforming Each Dictionary Key
- How To Generate An XML File As A Target Datastore Using ODI In An Integration Interface ? [ID 454268
- How to create an IPA on Xcode 4.3.2(打包成ipa)
- How to overlay an icon over existing shell objects
- How to bind the local files to TFS Server
- About UID and How to autostart an application on boot up in 3rd- Startup List Management API
- How to make an Android Spinner with initial text “Select One”
- How to enable an ASP.Net application to run on a SharePoint virtual server
- How to use Jackson to deserialise an array of objects
- How to display an empty row when the underlying dataset is empty
- How to convert an OpenCV IplImage to an SDL_Surface?
- How to generate an AP debug log file
- How to add an ActiveX Control