Transferred from: sunrack
Convert List <T> to BindingList <T>, and set the DataSource of the DataGridView to BindingList <T> !!
Code:
DataGridView.DataSource = new BindingList<T>(List<T>);
Convert the DataSource bound to the BindingList <T> to the List <T>. Likewise
Code:
List<T> modelList=new List<T>((BindingList<T>)this.DataGridView.DataSource);
Note: Both BindingList <T> and List <T> have constructor. The parameter is IEnumerable <T>. since both of them inherit IEnumerable, they can convert each other.
The following is the execution process of the constructor:
Public List (IEnumerable <T> collection)
{
If (collection = null)
{
ThrowHelper. ThrowArgumentNullException (ExceptionArgument. collection );
}
ICollection <T> is2 = collection as ICollection <T>;
If (is2! = Null)
{
Int count = is2.Count;
This. _ items = new T [count];
Is2.CopyTo (this. _ items, 0 );
This. _ size = count;
}
Else
{
This. _ size = 0;
This. _ items = new T [4];
Using (IEnumerator <T> enumerator = collection. GetEnumerator ())
{
While (enumerator. MoveNext ())
{
This. Add (enumerator. Current );
}
}
}
}