Code Snippet
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows.Forms;
namespace WindowsApplication1
{
public class Form1 : Form
{
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
public Form1()
{
InitializeComponent();
}
testCollection _collection;
private void Form1_Load(object sender, EventArgs e)
{
_collection = new testCollection();
bindingSource1.DataSource = _collection;
}
private void button1_Click(object sender, EventArgs e)
{
_collection.Add(new testClass());
}
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.dataGridView1 = new System.Windows.Forms.DataGridView();
this.button1 = new System.Windows.Forms.Button();
this.xDataGridViewTextBoxColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
this.yDataGridViewTextBoxColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
this.strDataGridViewTextBoxColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
this.bindingSource1 = new System.Windows.Forms.BindingSource(this.components);
((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.bindingSource1)).BeginInit();
this.SuspendLayout();
//
// dataGridView1
//
this.dataGridView1.AllowUserToAddRows = false;
this.dataGridView1.AllowUserToDeleteRows = false;
this.dataGridView1.AutoGenerateColumns = false;
this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
this.dataGridView1.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
this.xDataGridViewTextBoxColumn,
this.yDataGridViewTextBoxColumn,
this.strDataGridViewTextBoxColumn});
this.dataGridView1.DataSource = this.bindingSource1;
this.dataGridView1.Location = new System.Drawing.Point(12, 12);
this.dataGridView1.Name = "dataGridView1";
this.dataGridView1.Size = new System.Drawing.Size(705, 345);
this.dataGridView1.TabIndex = 0;
//
// button1
//
this.button1.Location = new System.Drawing.Point(12, 363);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 1;
this.button1.Text = "Add";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// xDataGridViewTextBoxColumn
//
this.xDataGridViewTextBoxColumn.DataPropertyName = "X";
this.xDataGridViewTextBoxColumn.HeaderText = "X";
this.xDataGridViewTextBoxColumn.Name = "xDataGridViewTextBoxColumn";
//
// yDataGridViewTextBoxColumn
//
this.yDataGridViewTextBoxColumn.DataPropertyName = "Y";
this.yDataGridViewTextBoxColumn.HeaderText = "Y";
this.yDataGridViewTextBoxColumn.Name = "yDataGridViewTextBoxColumn";
//
// strDataGridViewTextBoxColumn
//
this.strDataGridViewTextBoxColumn.DataPropertyName = "Str";
this.strDataGridViewTextBoxColumn.HeaderText = "Str";
this.strDataGridViewTextBoxColumn.Name = "strDataGridViewTextBoxColumn";
//
// bindingSource1
//
this.bindingSource1.DataSource = typeof(WindowsApplication1.testClass);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(729, 452);
this.Controls.Add(this.button1);
this.Controls.Add(this.dataGridView1);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.bindingSource1)).EndInit();
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.DataGridView dataGridView1;
private System.Windows.Forms.BindingSource bindingSource1;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.DataGridViewTextBoxColumn xDataGridViewTextBoxColumn;
private System.Windows.Forms.DataGridViewTextBoxColumn yDataGridViewTextBoxColumn;
private System.Windows.Forms.DataGridViewTextBoxColumn strDataGridViewTextBoxColumn;
}
public class testCollection : IBindingList
{
private IList<testClass> _list;
public testCollection()
{
_list = new List<testClass>();
}
#region IBindingList Members
public void AddIndex(PropertyDescriptor property)
{
throw new Exception("The method or operation is not implemented.");
}
public object AddNew()
{
return new testClass();
}
public bool AllowEdit
{
get { return true; }
}
public bool AllowNew
{
get { return true; }
}
public bool AllowRemove
{
get { return true; }
}
public void ApplySort(PropertyDescriptor property, ListSortDirection direction)
{
throw new Exception("The method or operation is not implemented.");
}
public int Find(PropertyDescriptor property, object key)
{
throw new Exception("The method or operation is not implemented.");
}
public bool IsSorted
{
get { throw new Exception("The method or operation is not implemented."); }
}
public event ListChangedEventHandler ListChanged;
public void RemoveIndex(PropertyDescriptor property)
{
throw new Exception("The method or operation is not implemented.");
}
public void RemoveSort()
{
throw new Exception("The method or operation is not implemented.");
}
public ListSortDirection SortDirection
{
get { throw new Exception("The method or operation is not implemented."); }
}
public PropertyDescriptor SortProperty
{
get { throw new Exception("The method or operation is not implemented."); }
}
public bool SupportsChangeNotification
{
get { return true; }
}
public bool SupportsSearching
{
get { return false; }
}
public bool SupportsSorting
{
get { return false; }
}
#endregion
#region IList Members
public int Add(object value)
{
_list.Add((testClass)value);
if (ListChanged != null)
ListChanged(this, new ListChangedEventArgs(ListChangedType.ItemAdded, _list.Count - 1));
return _list.Count - 1;
}
public void Clear()
{
throw new Exception("The method or operation is not implemented.");
}
public bool Contains(object value)
{
throw new Exception("The method or operation is not implemented.");
}
public int IndexOf(object value)
{
throw new Exception("The method or operation is not implemented.");
}
public void Insert(int index, object value)
{
throw new Exception("The method or operation is not implemented.");
}
public bool IsFixedSize
{
get { throw new Exception("The method or operation is not implemented."); }
}
public bool IsReadOnly
{
get { throw new Exception("The method or operation is not implemented."); }
}
public void Remove(object value)
{
throw new Exception("The method or operation is not implemented.");
}
public void RemoveAt(int index)
{
throw new Exception("The method or operation is not implemented.");
}
public object this[int index]
{
get
{
return _list[index];
}
set
{
_list[index] = (testClass)value;
}
}
#endregion
#region ICollection Members
public void CopyTo(Array array, int index)
{
throw new Exception("The method or operation is not implemented.");
}
public int Count
{
get { return _list.Count; }
}
public bool IsSynchronized
{
get { throw new Exception("The method or operation is not implemented."); }
}
public object SyncRoot
{
get { throw new Exception("The method or operation is not implemented."); }
}
#endregion
#region IEnumerable Members
public System.Collections.IEnumerator GetEnumerator()
{
return _list.GetEnumerator();
}
#endregion
}
public class testClass
{
public testClass()
{
_x = 0;
_y = 1;
_str = "grrr";
}
int _x;
public int X
{
get { return _x; }
set { _x = value; }
}
int _y;
public int Y
{
get { return _y; }
set { _y = value; }
}
string _str;
public string Str
{
get { return _str; }
set { _str = value; }
}
}
}