With or without you
爆发
社区
新随笔
联系
管理
随笔- 22 文章- 0 评论- 4
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);
}
}
}
}
posted @ 2008-03-26 06:50
one
阅读(393)
评论(4)
编辑
收藏
所属分类:
code
发表评论
回复
引用
#1楼
2008-03-26 08:05 |
chao1573> [未注册用户]
datakey?
回复
引用
查看
#2楼
2008-06-12 15:36 |
ylhyh
不错,刚好遇到了此问题
回复
引用
#3楼
2008-06-15 11:46 |
sadfas [未注册用户]
fsadfsadfsdf
回复
引用
#4楼
2008-07-03 07:11 |
TKer [未注册用户]
有是有了.不过一点的话报错“集合是只读的”.......
新用户注册
刷新评论列表
标题
姓名
主页
Email
(博主才能看到)
验证码
*
看不清,换一张
[
登录
][
注册
]
内容(请不要发表任何与政治相关的内容)
网站首页
新闻频道
社区
小组
博问
网摘
闪存
找找看
Remember Me?
登录
使用高级评论
新用户注册
返回页首
恢复上次提交
[使用Ctrl+Enter键可以直接提交]
该文被作者在 2008-03-26 15:59 编辑过
相关文章:
LINQ to SQL语句(25)之继承
走进Linq-Linq to Objects(下)实例篇
MSN Messenger 无法登陆的另类解决方法
关于:以前的某个程序安装已在安装计算机上创建挂起的文件操作 解决办法
google code上的开源项目如何下载?
数据库题目求解
code 128码介绍
关于区分CODE128和EAN128码
相关链接:
所属分类的其他文章:
获得以TestMethodAttribue为属性的方法的名称
转载:[你必须知道的.NET] 第一回:恩怨情仇:is和as
一个小项目的苦恼
DataGridView绑定List
时无法进行添加删除操作的解决方法
读 (dot NET Test Automation Recipes A Problem Solution Approach)
最新IT新闻:
Google向Wine贡献更多代码
苹果创始人Steve Wozniak称iPod将被淘汰
福布斯:硅谷日子将更难过 谁下一个倒下
微软老矣 尚能赚否?
YouTube探索新赢利模式 开始售音乐、视频游戏
<
2008年7月
>
日
一
二
三
四
五
六
29
30
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
1
2
3
4
5
6
7
8
9
与我联系
发短消息
搜索
留言簿
给我留言
查看留言
随笔分类
code(6)
(rss)
Life(3)
(rss)
随笔档案
2008年8月 (1)
2008年7月 (5)
2008年6月 (2)
2008年4月 (1)
2008年3月 (8)
2007年10月 (1)
2007年9月 (1)
文章分类
AJAX
(rss)
Asp.net MVC
(rss)
Dot Net 3.5
(rss)
Xml
(rss)
最新随笔
1. 获得以TestMethodAttribue为属性的方法的名称
2. 转载:[你必须知道的.NET] 第一回:恩怨情仇:is和as
3. 一个小项目的苦恼
4. Unable to find manifest signing certificate in the certificate store.
5. Static
6. Const and readonly
7. 7月IELTS
8. 34个加分写作句型
9. 无奈的代码(同事小朱)
10. DataGridView绑定List时无法进行添加删除操作的解决方法
积分与排名
积分 - 803
排名 - 10389
最新评论
阅读排行榜
1. DataGridView绑定List
时无法进行添加删除操作的解决方法(393)
2. 多频同义词(38)
3. 7月IELTS(33)
4. 一个小项目的苦恼(31)
5. 无奈的代码(同事小朱)(23)
评论排行榜
1. DataGridView绑定List
时无法进行添加删除操作的解决方法(4)
2. 无奈的代码(同事小朱)(0)
3. 34个加分写作句型(0)
4. 7月IELTS(0)
5. Const and readonly(0)