C# DataGridView 选中所有复选框

news2025/7/17 0:37:39

问题描述 

        在程序中尝试选中所有复选框,但出现错误。如果单击顶部的完整选中/释放复选框,同时选中包含复选框的列,则选定区域不会改变。该如何解决?

上面的图片是点击完整版本之后的。

下面是本文的测试代码,函数 dataGridView1_CellPainting() 和 dgvCheckBox_CheckedChanged() 用于完整检查/释放操作。

namespace TestWinForm
{
    public partial class Form1 : Form
    {
        List<string> saved_file_names = new List<string>();
        int table_index = 0;

        public Form1()
        {
            InitializeComponent();
        }

        private void add_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Title = "文本";
            ofd.Filter =  "txt | *.txt";
            ofd.Multiselect = true; // 

            DialogResult dr = ofd.ShowDialog();

            if (dr == DialogResult.OK)
            {
                foreach (string file_name in ofd.FileNames)
                {
                    //  1. 
                    if (saved_file_names.Contains(file_name))
                        continue;

                    //  2. 
                    dataGridView1.Rows.Add();
                    dataGridView1.Rows[table_index].Cells[1].Value = table_index + 1;
                    dataGridView1.Rows[table_index].Cells[2].Value = file_name;
                    saved_file_names.Add(file_name);
                    dataGridView1.Rows[table_index].Cells[3].Value = "none";
                    table_index++;
                }
            }
        }

        private void delete_Click(object sender, EventArgs e)
        {
            bool is_checked = false;
            List<int> delete_index = new List<int>();
            for (int i = 0; i < table_index; i++)
            {
                if (Convert.ToBoolean(dataGridView1.Rows[i].Cells[0].Value) == true)
                    delete_index.Add(i);
            }

            if (delete_index.Count == 0)
                return;

            delete_index.Reverse();

            foreach (var index in delete_index)
            {
                table_index--;
                saved_file_names.RemoveAt(index);
                dataGridView1.Rows.RemoveAt(index);
            }
        }

        private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
        {
            if (e.ColumnIndex == 0 && e.RowIndex == -1)
            {
                e.PaintBackground(e.ClipBounds, false);
                Point pt = e.CellBounds.Location;

                int nChkBoxWidth = 15;
                int nChkBoxHeight = 15;
                int offsetx = (e.CellBounds.Width - nChkBoxWidth) / 2;
                int offsety = (e.CellBounds.Height - nChkBoxHeight) / 2;

                pt.X += offsetx;
                pt.Y += offsety;

                CheckBox cb = new CheckBox();

                cb.Size = new Size(nChkBoxWidth, nChkBoxHeight);
                cb.Location = pt;
                cb.Checked = true;
                cb.CheckedChanged += new EventHandler(dgvCheckBox_CheckedChanged);

                ((DataGridView)sender).Controls.Add(cb);

                e.Handled = true;

            }

        }

        private void dgvCheckBox_CheckedChanged(object sender, EventArgs e)
        {
            foreach (DataGridViewRow r in dataGridView1.Rows)
            {
                r.Cells[0].Value = ((CheckBox)sender).Checked;
            }
        }

    }
}

解决方法

        请考虑使用数据绑定,DataGridView这可以简化您的操作。通过定义一个Record包含属性的类,当您将该记录添加到这些记录的bool IsChecked绑定列表时,复选框行会自动创建。然后,您可以通过在记录中设置属性来操作该复选框,而无需调用 UI 对象本身。

单击标题单元格:

        如果全部选中或未选中,则全部将切换。

        如果混合了已选中和未选中,则所有都将被提升为已选中。

示例代码:

using System;
using System.ComponentModel;
using System.Linq;
using System.Windows.Forms;

namespace dgv_ac
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Wait for the main form to be created, then attach 
        /// your Binding List as the data source of the DGV
        /// </summary>
        protected override void OnHandleCreated(EventArgs e)
        {
            base.OnHandleCreated(e);
            dataGridView1.DataSource = this.DataSource;
            initDGV();
        }

        private void initDGV()
        {
            dataGridView1.AllowUserToAddRows = false;

            // Now you can populate the DataGridView simply
            // by adding some records to the list.
            for (int i = 0; i < 5; i++)
            {
                DataSource.Add(new Record { Number = i, FileName = $"MyFile_{i}.txt" });
            }


            // Once the first record is added, the Columns information is
            // available and we can do column formatting.
            dataGridView1.Columns[nameof(Record.FileName)].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
            var checkboxColumn = dataGridView1.Columns[nameof(Record.IsChecked)];
            checkboxColumn.HeaderText = string.Empty;
            checkboxColumn.Width = 40;
            dataGridView1.CellClick += onCellClick;
            dataGridView1.CellContentClick += onCellContentClick;
        }

        /// <summary>
        /// Detect check box click and end the edit mode in this case.
        /// </summary>
        private void onCellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if(e.RowIndex != -1)
            {
                var cell = dataGridView1[e.ColumnIndex, e.RowIndex];
                if(cell is DataGridViewCheckBoxCell checkbox)
                {
                    dataGridView1.EndEdit();
                }
            }
        }


        /// <summary>
        /// Detect header click and set the records accordingly.
        /// </summary>
        private void onCellClick(object sender, DataGridViewCellEventArgs e)
        {
            if(e.RowIndex == -1)
            {
                switch (dataGridView1.Columns[e.ColumnIndex].Name)
                {
                    case nameof(Record.IsChecked):
                        if (DataSource.Any())   // Check to see if there are any records at all.
                        {
                            if(DataSource.Count(record=>record.IsChecked) == DataSource.Count)
                            {
                                // This block says thet're all chacked or all unchecked.
                                if(DataSource.First().IsChecked) // then they all are
                                {
                                    setAll(false);
                                }
                                else
                                {
                                    setAll(true);
                                }
                            }
                            else setAll(true); // If they're mixed, make them all checked.
                        }
                        break;
                }
            }
            void setAll(bool value)
            {
                foreach (var record in DataSource)
                {
                    record.IsChecked = value;
                }
                Refresh();
            }
        }

        public BindingList<Record> DataSource = new BindingList<Record>();
    }

    
    // This is the record class that will provide column 
    // information to the DataGridView automatically.
    public class Record
    {
        public int Number { get; set; }
        public bool IsChecked { get; set; }
        public string FileName { get; set; }
    }
}

另一种方法

这种方式,本文未测试:

 private void dgvCheckBox_CheckedChanged(object sender, EventArgs e)
        {
            foreach (DataGridViewRow r in dataGridView1.Rows)
            {
                if (r.Cells[0] is DataGridViewCheckBoxCell checkbox)
                {
                    dataGridViewDocuments.EndEdit();
                }
                r.Cells[0].Value = ((CheckBox)sender).Checked;
            }
        } 

如果您喜欢此文章,请收藏、点赞、评论,谢谢,祝您快乐每一天。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2377643.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

AB Download Manager v1.5.8 开源免费下载工具

下载文件是我们日常工作和生活中经常进行的操作。面对动辄数十GB的4K影片、设计素材包或开发工具&#xff0c;传统浏览器的单线程下载如同"涓涓细流"&#xff0c;非常影响我们的效率和体验。 那么&#xff0c;一款高效且易用的下载工具至关重要。今天就让我们解锁这…

深度剖析:Dify+Sanic+Vue+ECharts 搭建 Text2SQL 项目 sanic-web 的 Debug 实战

目录 项目背景介绍sanic-web Dify\_service handle\_think\_tag报错NoneType问题描述debug Dify调用不成功&#xff0c;一直转圈圈问题描述debug 前端markdown格式只显示前5页问题描述debug1. 修改代码2.重新构建1.1.3镜像3.更新sanic-web/docker/docker-compose.yaml4. 重新部…

学习51单片机02

吐血了&#xff0c;板子今天才到&#xff0c;下午才刚开始学的&#xff0c;生气了&#xff0c;害我笔记都断更了一天。。。。 紧接上文...... 如何将HEX程序烧写到程序? Tips&#xff1a;HEX 文件是一种常用于单片机等嵌入式系统的文件格式&#xff0c;它包含了程序的机器码…

麒麟服务器操作系统安装 MySQL 8 实战指南

往期好文连接&#xff1a;统信UOS/麒麟KYLINOS安装JDBC驱动包 Hello&#xff0c;大家好啊&#xff0c;今天给大家带来一篇麒麟服务器操作系统上安装 MySQL 8 的文章&#xff0c;欢迎大家分享点赞&#xff0c;点个在看和关注吧&#xff01;MySQL 作为主流开源数据库之一&#x…

AWS EC2 微服务 金丝雀发布(Canary Release)方案

为什么需要实现金丝雀发布? 在当前项目的工程实践中, 已经有了充分的单元测试, 预发布环境测试, 但是还是会在线上环境出现非预期的情况, 导致线上事故, 因此, 为了提升服务质量, 需要线上能够有一个预验证的机制. 如何实现金丝雀发布? 使用AWS code deploy方案 AWS code…

支持蓝牙5.0和2.4G私有协议芯片-PHY6222

PHY6222QC-W04C 是一款适用于蓝牙低功耗&#xff08;BLE&#xff09;5.2 应用的片上系统&#xff08;SoC&#xff09;。它搭载 ARM Cortex™-M0 32 位处理器&#xff0c;配备 64KB SRAM、512K Flash、96KB ROM、256 bit efuse &#xff0c;以及超低功耗、高性能的多模式射频模块…

图像锐化调整

一、背景介绍 之前找多尺度做对比度增强时候&#xff0c;发现了一些锐化相关算法&#xff0c;正好本来也要整理锐化&#xff0c;这里就直接顺手对之前做过的锐化大概整理了下&#xff0c;方便后续用的时候直接抓了。 这里整理的锐化主要是两块&#xff1a;一个是参考论文&#…

找客户的app

找客户的 app 在竞争激烈的商业环境中&#xff0c;找客户的 APP 成为企业拓展业务的利器。 微拓客 APP&#xff0c;集智能获客、营销素材、客户管理于一体。支持关键词、附近客源等多方式采集&#xff0c;覆盖 300 行业&#xff1b;一键采集客源&#xff0c;一键导出到通讯录…

​​金融合规革命:​​R²AIN SUITE 如何重塑银行业务智能​

一、市场发展背景与核心驱动因素​ 信息过载​&#xff1a;单家银行年均新增监管文件大量增加&#xff0c;人工解读效率极低。 客户体验升级​&#xff1a;高净值客户期待“724小时专业级响应”&#xff0c;但客户经理难以实时掌握数百款产品动态。 风险防控​&#xff1a;传…

论文阅读:Self-Collaboration Code Generation via ChatGPT

地址&#xff1a;Self-Collaboration Code Generation via ChatGPT 摘要 尽管大型语言模型&#xff08;LLMs&#xff09;在代码生成能力方面表现出色&#xff0c;但在处理复杂任务时仍存在挑战。在现实软件开发中&#xff0c;人类通常通过团队协作来应对复杂任务&#xff0c;…

2025年PMP 学习十五 第10章 项目资源管理

2025年PMP 学习十五 第10章 项目资源管理 序号过程过程组1规划沟通管理规划2管理沟通执行3监控沟通监控 项目沟通管理包括为确保项目的信 息及时且恰当地规划、收集、生成、发布、存储、检索、管理、控制、监 警和最终处理所需的过程&#xff1b; 项目经理绝大多数时间都用于与…

如何使用易路iBuilder智能体平台快速安全深入实现AI HR【实用帖】

随着企业组织经营对降本、增效、提质的需求日益迫切&#xff0c;越来越多企业启动人力资源数智化转型战略。而在AI战略实际推进过程中&#xff0c;企业组织往往在选型、搭建、使用、管控等问题上面临困惑&#xff1a; 如何快速、低成本接入AI能力&#xff0c;实现人力资源管理…

免费实用的远程办公方案​

假如你需要快速检索出远程电脑文件并下载&#xff1f; 假如你需要访问远程电脑的共享文件夹&#xff1f; 假如你需要访问远程电脑的USB设备&#xff0c;例如软件加密狗、调试器、固件烧录器、U盘等&#xff1f; 本篇文章能够解决以上痛点。 这个方案非常实用&#xff0c;也很…

【springboot项目服务假死、内存溢出问题排查】

问题现象&#xff1a;springboot服务A刚启动时正常&#xff0c;但运行几个小时后就会接口请求无响应&#xff0c;但服务器网络、磁盘I/O和CPU都没有出现爆满的情况&#xff0c;且A服务日志没有异常报错。 线上SpringBoot假死现象 SpringBoot应用会出现无法访问的情况。具体的表…

Java 线程状态详解:从创建到销毁的完整旅途

前言 在 Java 多线程编程中&#xff0c;线程的状态管理是理解并发逻辑的核心。本文将用通俗的语言和代码示例&#xff0c;解析线程的6种状态及其转换条件&#xff0c;助你彻底掌握线程的生命周期。 一、线程的6种状态 状态含义NEW线程对象已创建&#xff0c;但未启动&#xf…

操作系统|| 虚拟内存页置换算法

题目 写一个程序来实现 FIFO 和 LRU 页置换算法。首先&#xff0c;产生一个随机的页面引用序列&#xff0c;页面数从 0~9。将这个序列应用到每个算法并记录发生的页错误的次数。实现这个算法时要将页帧的数量设为可变。假设使用请求调页。可以参考所示的抽象类。 抽象类&…

Maven 项目构建时编译错误问题排查与解决

1. 问题描述 Maven 项目执行命令 mvn clean package 时出现编译错误&#xff0c;如下图所示 2. 问题分析 由于是源码编译错误&#xff0c;于是通过查看项目 pom.xml 文件&#xff0c;得到项目源码使用的 Java 版本为 21 <project xmlns"http://maven.apache.org/P…

安全生产调度管理系统的核心功能模块

安全生产调度管理系统是运用现代信息技术构建的智能化管理平台&#xff0c;旨在实现生产安全风险的全面管控和应急资源的优化调度。该系统通过整合物联网、大数据、人工智能等前沿技术&#xff0c;建立起覆盖风险监测、预警预测、指挥调度、决策支持的全链条安全管理体系。 一…

Linux进程信号(一)之信号的入门

文章目录 信号入门1. 生活角度的信号2. 技术应用角度的信号3. 注意4. 信号概念5.用kill -l命令可以察看系统定义的信号列表6. 信号处理常见方式 信号入门 1. 生活角度的信号 你在网上买了很多件商品&#xff0c;再等待不同商品快递的到来。但即便快递没有到来&#xff0c;你也…

基于springboot+vue的机场乘客服务系统

开发语言&#xff1a;Java框架&#xff1a;springbootJDK版本&#xff1a;JDK1.8服务器&#xff1a;tomcat7数据库&#xff1a;mysql 5.7数据库工具&#xff1a;Navicat12开发软件&#xff1a;eclipse/myeclipse/ideaMaven包&#xff1a;Maven3.3.9 系统展示 用户管理 航班信…