zoukankan
html css js c++ java
DataGridView绑定List时无法进行添加删除操作的解决方法
将List<T>转换为BindingList<T>,然后设置DataGridView的DataSource为BindingList<T>!!
代码:
DataGridView.DataSource
=
new
BindingList
<
T
>
(List
<
T
>
);
将绑定BindingList<T>的DataSource转化为List<T>,同理
代码:
List
<
T
>
modelList
=
new
List
<
T
>
((BindingList
<
T
>
)
this
.DataGridView.DataSource);
说明:BindingList<T>和List<T>都有个构造函数,参数是
IEnumerable<T>,既然他们俩个都是继承
IEnumerable
,当然能相互转换。
下面是这个构造函数的执行过程:
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);
}
}
}
}
查看全文
相关阅读:
时间复杂度的分析
插入排序
maven中jar、war、pom的区别
Maven属性(properties)标签的使用
超级POM
maven 常用命令
Maven Pom文件标签详解
maven 查找依赖的办法
maven snapshot和release版本的区别
maven pom文件标签含义
原文地址:https://www.cnblogs.com/yoshirogu/p/1564846.html
最新文章
Python学习笔记之导入模块
Python学习笔记控制之for循环和while循环
SIP协议入门:初学者必须明白的几个重要概念
SIP中From ,Contact, Via 和 Record-Route/Route
基于SDP的提议/应答(offer/answer)模型简介
SIP 3pcc
SIP中第三方呼叫控制(3PCC)建立流程
Session Timer机制分析
SIP中的SDP offer/answer交换初探
SIP:100rel 扩展
热门文章
SIP协议参数详解
【转】[STL]vector和deque的内存释放(clear)
删除Lb重复的数,用La输出(顺序表)
求新的集合 A=AUB(顺序表)
顺序表中基本操作的实现
堆排序的进一步理解
白话经典算法系列之七 堆与堆排序(转)
归并排序的进一步理解
白话经典算法系列之五 归并排序的实现(转)
快速排序
Copyright © 2011-2022 走看看