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);
}
}
}
}
查看全文
相关阅读:
android头像更换(实现拍照和从手机图片里选择两种形式)
安卓开发实战-记账本APP(六)
安卓开发实战-记账本APP(五)
安卓开发实战-记账本APP(四)
安卓开发实战-记账本APP(三)
BaseAdapter的三种表达式分析,startActivityForResult的使用
使用Bundle在Activity之间交换数据
深入理解JVM-类加载器深入解析(3)
深入理解java内存模型--读书笔记
深入理解JVM-类加载器深入解析(2)
原文地址:https://www.cnblogs.com/yoshirogu/p/1564846.html
最新文章
Spring优雅整合Redis缓存
大厂 Redis 性能优化的 13 条军规!收好了
一篇文章认识Gradle的使命
Gradle 如何打包 Spring Boot 可执行 JAR
【Gradle】IDEA Gradle使用Maven仓库的方法
Gradle的下载与安装
idea下springBoot+mybatis的逆向工程
C语言时间操作
C语言目录和文件操作扩展
C语言获取系统错误
热门文章
C语言编译预处理
C语言gdb调试
C语言makefile文件
从C到C++
C++函数重载
C++类和对象
C++类的详解
STL用法总结
构建之法读书笔记(一)
Android头像更换之详细操作
Copyright © 2011-2022 走看看