zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    多点触控(包括拖拽控件)的例子
    绑定当前对象例子——Tag="{Binding}"
    绑定自己Self
    按键(ESC ,F1,F2等)——wpf的命令处理方法
    C#基础:值类型、引用类型与ref关键字
    生成事件命令
    Prism——Region
    组合模式的一个案例说明
    Laravel 学习记录
    【Linux学习】3.Linux常见配置文件
  • 原文地址:https://www.cnblogs.com/markli/p/6053262.html
Copyright © 2011-2022 走看看