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;                                                   //设置列宽
            }
    
  • 相关阅读:
    SpringSecurity (Spring权限验证)
    Spring mvc Session拦截器
    判断是否登录的拦截器SessionFilter
    Jquery绑定多个BUTTON 点击事件
    jquery ajax提交表单数据的两种方式
    ASP小贴士/ASP Tips
    遍历组合的实现——VB2005
    应用程序生命周期(墓碑机制(程序和页面))【WP7学习札记之十一】
    反应性扩展框架(Reactive Extensions)【WP7学习札记之十六】
    ASP.NET 4【MSDN参考文档方便自己查阅】
  • 原文地址:https://www.cnblogs.com/feiyucha/p/10206656.html
Copyright © 2011-2022 走看看