zoukankan      html  css  js  c++  java
  • C# DataGridViewCheckboxCell usage

    如何获取DataGridViewCheckBoxCell的值

    方法1,需要给DATAGRIDVIEW控件添加事件CellContentClick,这里我将CHECKBOX放在数据列的第一个所以我判断参数中COLUMNINDEX是否为0,同时需要注意的是使用属性EditedFormattedValue,不能使用Value值,因为Value值是旧值。

            private void dgvABList_CellContentClick(object sender, DataGridViewCellEventArgs e)
            {
                if (e.ColumnIndex == 0 && e.RowIndex != -1)
                {
                    bool bSelect = Convert.ToBoolean(dgvABList[e.ColumnIndex, e.RowIndex].EditedFormattedValue);
                    string id = dgvABList[1, e.RowIndex].Value.ToString();
                    if (bSelect == true && m_delABs.Contains(id) == false)
                    {
                        m_delABs.Add(id);
                    }
                    if (bSelect == false && m_delABs.Contains(id) == true)
                    {
                        m_delABs.Remove(id);
                    }
                }
            }

    方法2:, 就是通过遍历整个DATAGRIDVIEW中的所有行来获取所有选上的数据,

                List<int> selected = new List<int>();
                foreach (DataGridViewRow row in dgvABList.Rows)
                { 
                    if (Convert.ToBoolean(row.Cells[0].Value) == true)
                    {
                        selected.Add(row.Index);
                    }
                }

    关键都是使用了Converter的ToBoolean函数来将object的true或者false转换成bool型。

  • 相关阅读:
    1010每次备份我的MySQL数据库
    1008win7与虚拟机中的linux共享文件的(详细)方法
    0930MySQL中实现高性能高并发计数器方案(例如文章点击数)
    0929shell操作mysql
    0929mysql前缀索引如何找到合适的位数
    0929mysql 用户管理和权限设置
    学习笔记之机器学习实战 (Machine Learning in Action)
    学习笔记之Python for Data Analysis
    学习笔记之入行数据科学,这些书一定要看
    面试总结之Python
  • 原文地址:https://www.cnblogs.com/rogerroddick/p/2982274.html
Copyright © 2011-2022 走看看