• 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
  • 相关阅读:
    web学习---html,js,php,mysql一个动态网页获取流程
    用正则表达式做替换
    Array.prototype.slice.call(arguments)
    javascript join以及slice,push函数
    SQLServer中跨服务器跨数据库之间的数据操作
    SQL Server里面如何导出包含数据的SQL脚本
    Sql Server 常用日期格式
    Microsoft SQL Server下的SQL语句
    多线程与UI操作(二)
    多线程与UI操作(一)
  • 原文地址:https://www.cnblogs.com/markli/p/6053262.html
Copyright © 2020-2023  润新知