zoukankan      html  css  js  c++  java
  • 为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;                                                   //设置列宽
            }
    
  • 相关阅读:
    android 项目
    input keyevent 数字对应的操作
    logcat 使用方法
    android查看内存使用情况
    图片点击放大效果
    禁止img图片拖动在新窗口打开
    人工智能
    游戏开发
    随手做的一个模拟弹出窗口
    Html的<meta>标签使用方法及用例
  • 原文地址:https://www.cnblogs.com/feiyucha/p/10206656.html
Copyright © 2011-2022 走看看