DataGridView HeadCell 添加 CheckBox

添加CheckBox

#region headCell 添加 CheckBox
        public static CheckBox ckBox = new System.Windows.Forms.CheckBox();
        public static System.Windows.Forms.DataGridView dgv;
        public static void AddFullSelect()
        {
            ckBox.Text = "colsCk";
            ckBox.Checked = false;
            System.Drawing.Rectangle rect = dgv.GetCellDisplayRectangle(0, -1, true);
            ckBox.Size = new System.Drawing.Size(13, 13);
            ckBox.Location = new Point(rect.Location.X + dgv.Columns[0].Width / 2 - 13 / 2 - 1, rect.Location.Y + 3);
            //ckBox.Location.Offset(-40, rect.Location.Y);
            ckBox.CheckedChanged += new EventHandler(ckBox_CheckedChanged);
            dgv.Controls.Add(ckBox);
        }
        static void ckBox_CheckedChanged(object sender, EventArgs e)
        {
            for (int i = 0; i < dgv.Rows.Count; i++)
            {
                dgv.Rows[i].Cells["colselect"].Value = ((System.Windows.Forms.CheckBox)sender).Checked;
            }
            dgv.EndEdit();
        }

        #endregion

窗体加载事件

 dgv = dgvResult;
            AddFullSelect();

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在C#的DataGridView中判断checkbox的状态,可以通过以下几种方法实现。 方法一:遍历DataGridView的每一行,判断每一行的checkbox是否被选中。可以使用循环来遍历每一行,并通过Cells属性来获取每一行的checkbox单元格的值。例如,可以使用以下代码来判断checkbox的状态: ```csharp for (int i = 0; i < dataGridView_tabPage1.Rows.Count; i++) { bool isChecked = (bool)dataGridView_tabPage1.Rows\[i\].Cells\[0\].Value; if (isChecked) { // checkbox被选中的处理逻辑 } else { // checkbox未被选中的处理逻辑 } } ``` 方法二:使用事件处理程序来判断checkbox的状态。可以为DataGridViewCellValueChanged事件添加事件处理程序,在事件处理程序中判断checkbox的状态。例如,可以使用以下代码来判断checkbox的状态: ```csharp private void dataGridView_tabPage1_CellValueChanged(object sender, DataGridViewCellEventArgs e) { if (e.ColumnIndex == 0 && e.RowIndex >= 0) { bool isChecked = (bool)dataGridView_tabPage1.Rows\[e.RowIndex\].Cells\[0\].Value; if (isChecked) { // checkbox被选中的处理逻辑 } else { // checkbox未被选中的处理逻辑 } } } ``` 以上是两种常用的方法来判断DataGridViewcheckbox的状态。根据你的具体需求,选择适合的方法来判断checkbox的状态即可。\[1\]\[2\]\[3\] #### 引用[.reference_title] - *1* *2* *3* [Datagridview 添加checkbox列,并判断Datagridview 中的checkbox列是否被选中](https://blog.csdn.net/fangyuan621/article/details/118156656)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值