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);
}
}
}
}
查看全文
相关阅读:
你不会Python这几个库,不要说你会爬虫
如何用Python破解验证码,适合新手练手
2020最新Python入门笔记
10个超有趣的Python项目,你会哪个?
Python编程必学规范【新手必学】
你对Python变量理解到位了没有,80%的人都不知道
这个男人让你的python爬虫开发效率提升8倍
Python抖音机器人,论如何在抖音上找到漂亮小姐姐?
在Python中实现函数重载,60%的人都不会
2020十大Python面试题,你会几个?
原文地址:https://www.cnblogs.com/yoshirogu/p/1564846.html
最新文章
c#:HttpClient加标头
UI5-文档-4.30-Debugging Tools
UI5-文档-4.29-Integration Test with OPA
UI5-文档-4.28-Unit Test with QUnit
UI5-文档-4.27-Mock Server Configuration
UI5-文档-4.26-(Optional) Remote OData Service
UI5-文档-4.25-Sorting and Grouping
UI5-文档-4.24-Filtering
UI5-文档-4.23-Custom Formatters
UI5-文档-4.22-Expression Binding
热门文章
UI5-文档-4.21-Data Types
《ASP.NET SignalR系列》第三课 SignalR的支持平台
《ASP.NET SignalR系列》第二课 SignalR的使用说明
《ASP.NET SignalR系列》第一课 认识SignalR
MVC5使用SignalR进行双向通信 (1)
[老老实实学WCF] 第十篇 消息通信模式(下) 双工
[老老实实学WCF] 第九篇 消息通信模式(上) 请求应答与单向
[老老实实学WCF] 第七篇 会话
[老老实实学WCF] 第八篇 实例化
[老老实实学WCF] 第六篇 元数据交换
Copyright © 2011-2022 走看看