为DataGridView控件实现复选功能
实现效果:

知识运用:
DataGridViewCheckBoxColumn类
实现代码:
private class Fruit
{
public int Price { get; set; }
public string Name { get; set; }
public bool ft;
} private List<Fruit> P_fruit;
private void Form1_Load(object sender, EventArgs e)
{
DataGridViewCheckBoxColumn dgvc = new DataGridViewCheckBoxColumn(); //创建列对象
dgvc.HeaderText = "状态"; //设置列标题
dataGridView1.Columns.Add(dgvc); //添加列
P_fruit = new List<Fruit>() //创建数据集合
{
new Fruit(){Price=21,Name="水蜜桃"},
new Fruit(){Price=33,Name="榴莲"},
new Fruit(){Price=24,Name="柑橘"},
new Fruit(){Price=22,Name="黄柠檬"},
new Fruit(){Price=21,Name="紫葡萄"}
};
dataGridView1.DataSource = P_fruit; //绑定数据集合
dataGridView1.Columns[0].Width = 50; //设置列宽
dataGridView1.Columns[1].Width = 140; //设置列宽
dataGridView1.Columns[2].Width = 150; //设置列宽
} private void btn_remove_Click(object sender, EventArgs e)
{
for (int i = 0; i < dataGridView1.Rows.Count; i++) //遍历行集合
{
if (dataGridView1.Rows[i].Cells[0].Value != null && dataGridView1.Rows[i].Cells[1].Value != null &&
dataGridView1.Rows[i].Cells[2].Value != null) //判断值是否为空
{
if (Convert.ToBoolean(dataGridView1.Rows[i].Cells[0].Value.ToString())) //判断是否选中项
{
P_fruit.RemoveAll( //标记集合中的指定项
(pp) =>
{
if (pp.Name == dataGridView1.Rows[i].Cells[2].Value.ToString() &&
pp.Price == Convert.ToSingle(dataGridView1.Rows[i].Cells[1].Value.ToString()))
pp.ft = true; //开始标记
return false; //不删除项
});
}
}
}
P_fruit.RemoveAll( //删除集合中的指定项
(pp) =>
{
return pp.ft;
});
dataGridView1.DataSource = null; //绑定为空
dataGridView1.DataSource = P_fruit; //绑定到数据集合
dataGridView1.Columns[0].Width = 50; //设置列宽
dataGridView1.Columns[1].Width = 140; //设置列宽
dataGridView1.Columns[2].Width = 150; //设置列宽
}
为DataGridView控件实现复选功能的更多相关文章
- DevExpress TreeList控件的复选框
作者:jiankunking 出处:http://blog.csdn.net/jiankunking TreeList控件能够同一时候显示树结构和其它数据列,即在一个列上建立父子关系展开或收缩,同一时 ...
- 获取ListControl控件中(复选框)CheckBox的状态
原文地址:http://blog.chinaunix.net/uid-20680966-id-1896376.html 1 建立测试工程 新建一个对话框工程,并添加一个CListCtrl控件 ...
- jQueryMobile控件之复选框
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- [asp.net] 通过JS实现对treeview控件的复选框单选控制。
前端JS代码: //识别不同的浏览器 function getTargetElement(evt) { var elem if (evt.target) { elem = (evt.target.no ...
- VS2010,VS2013 Datagridview控件的编辑列功能,弹窗界面被挤扁了
搜了很久,没找到解决办法,在一个角落看到说要卸载Framework,实践后可以,发出来记一下. 解决办法: 发现自己电脑上多了Framework4.8,可能安装VS2013的时候自动安装的. 卸载了F ...
- Flex 扩展combobox 实现复选功能
前段时间,老大召唤,给个任务,研究一下flex的combobox控件 实现复选功能(flex自带的combobox控件不可以复选),小兵能力有限,哪里可以扩展呀,网上看了别人写的,发现总是有点瑕疵(关 ...
- QT 创建一个具有复选功能的下拉列表控件
最近研究了好多东西,前两天突然想做一个具有复选功能的下拉列表框.然后在网上"学习"了很久之后,终于发现了一个可以用的,特地发出来记录一下. 一.第一步肯定是先创建一个PROJECT ...
- 基于Winform框架DataGridView控件的SqlServer数据库查询展示功能的实现
关键词:Winform.DataGridView.SqlServer 一个基于winform框架的C/S软件,主要实现对SqlServer数据库数据表的实时查询. 一.为DataGridView添加数 ...
- Datagridview控件实现分页功能
可以进行sql语句进行设置: 1.先新建一个窗体,一个DataGridView控件.两个label控件.两个Button控件 2.代码如下: using System; using Sy ...
随机推荐
- time元素 pubdate属性
time元素和pubdate属性 https://blog.csdn.net/ziy10231207/article/details/51883397
- 938. Range Sum of BST
Given the root node of a binary search tree, return the sum of values of all nodes with value betwee ...
- 732. My Calendar III (prev)
Implement a MyCalendarThree class to store your events. A new event can always be added. Your class ...
- [51nod] 1007 正整数分组 dp
将一堆正整数分为2组,要求2组的和相差最小. 例如:1 2 3 4 5,将1 2 4分为1组,3 5分为1组,两组和相差1,是所有方案中相差最少的. Input 第1行:一个数N,N为正整数的数量 ...
- 51nod1110(xjb)
题目链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1110 题意:中文题诶- 思路:可以将在 xi 位置,权值为 w ...
- jzoj6008. 【THUWC2019模拟2019.1.18】Sequence (矩阵加速)
题面 茉优最近研究发现,一个人的想愿能力可以认为是字符串S的一个子串S[l,r],而连接值可以认为是这个子串的本质不同子序列个数.现在她想验证她的结论是否正确,于是她给了你Q个询问,希望你帮她来计算, ...
- Canesten 项目申请ICP备案过程中遇到的问题及知识点扩充
遇到的问题:Canesten备案主体是Bayer *** Ltd., Shanghai Branch, 而提供的IP是 Bayer (China)limited,违背了电信要求的三一致原则,即备案主体 ...
- 基于testcontainers的现代化集成测试进阶之路
大型的软件工程项目除了大量的产品级代码外必不可少的还有大量的自动化测试.自动化测试包含从前端到后端甚至到产品线上不同模块和环境的各种类型的测试.一个比较经典的关于自动化测试分布的理论就是测试金字塔,是 ...
- Maven打包Spark程序Pom配置
scala和java混合的spark程序之前使用其他配置始终有报找不到包的情况,尝试了一下如下配置可以打包成功.<build> <pluginManagement> <p ...
- Codeforces Round #527-B. Teams Forming(贪心)
time limit per test 1 second memory limit per test 256 megabytes input standard input output standar ...