DataGridView里CheckBox实现全选控制

1、 checkbox点击事件

private void myStyleDataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == -1 || e.RowIndex == -1) return;
            if (this.myStyleDataGridView1.Columns[e.ColumnIndex].Name == "IsSelect")
            {
                if (this.myStyleDataGridView1[e.ColumnIndex, e.RowIndex].Value == null)
                {
                    this.myStyleDataGridView1.Rows[e.RowIndex].Cells["IsSelect"].Value = false;
                }
                if (this.myStyleDataGridView1[e.ColumnIndex, e.RowIndex].Value.ToString().ToUpper() == "TRUE")
                {
                    this.myStyleDataGridView1[e.ColumnIndex, e.RowIndex].Value = false;
                    myStyleDataGridView1.SelectAll();
                }
                else
                {
                    this.myStyleDataGridView1[e.ColumnIndex, e.RowIndex].Value = true;
                }

            }
            getTotal();

        }
View Code

 

 

2、统计选中的行数

  /// <summary>
        /// 统计
        /// </summary>
        private int getTotal()
        {
            int mCount = 0;
            mYDNos = "";
            for (int i = 0; i < this.myStyleDataGridView1.RowCount; i++)
            {
                if ((myStyleDataGridView1.Rows[i].Cells["IsSelect"].Value + "").ToUpper() == "TRUE")
                {
                    mCount++;
                    mYDNos += "'" + myStyleDataGridView1.Rows[i].Cells["YDNo"].Value + "" + "',";
                }
            }
            mYDNos = mYDNos.TrimEnd(',');
            return mCount;
        }
View Code

 

 

 

3、全选

        /// <summary>
        /// 全选
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ck_All_Click(object sender, EventArgs e)
        {
            for (int count = 0; count < this.myStyleDataGridView1.Rows.Count; count++)
            {
                if (ck_All.Checked)
                {
                    this.myStyleDataGridView1.Rows[count].Cells["IsSelect"].Value = true;
                }
                else
                {
                    this.myStyleDataGridView1.Rows[count].Cells["IsSelect"].Value = false;
                }
            }
            getTotal();
        }
View Code

 

转载于:https://www.cnblogs.com/markli/p/6053262.html

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值