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

 

posted @ 2016-11-11 10:08  Mark1997  阅读(1411)  评论(0编辑  收藏  举报