zoukankan      html  css  js  c++  java
  • C# Windows DataGridView 判断CheckBox 选取的方法

    最近在做一个C# winform的小东西,在用到向DataWindow 中添加新行.实现方法是右击菜单后弹出一窗体,新窗体上有一个DataGridView ,第一列是个DataGridViewCheckBoxColumn列.要求是选中checkbox的行添加到父窗体数据源中.现就判断哪些有选中的


    foreach (DataGridViewRow dr in this.dataGridView1.Rows)
                {
                    try
                    {
                        //DataGridViewCheckBoxCell cbx = (DataGridViewCheckBoxCell)dr.Cells[0];
                        //if ((bool)cbx.FormattedValue)
                        if(dr.Cells[0].Selected)
                        {
                            arrShiftCode.Add(dr.Cells[1].Value);
                            arrShiftGroup.Add(dr.Cells[2].Value);
                        }
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                    }

                }


    以上是一开始这样写的,发现选中了多个,始终只有最后一个是True,其他的都是False.最后经查资料有如下写法即可      

                foreach (DataGridViewRow dr in this.dataGridView1.Rows)
                {
                    try
                    {
                        DataGridViewCheckBoxCell cbx = (DataGridViewCheckBoxCell)dr.Cells[0];
                        if ((bool)cbx.FormattedValue)
                        {
                            arrShiftCode.Add(dr.Cells[1].Value);
                            arrShiftGroup.Add(dr.Cells[2].Value);
                        }
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                    }
                }

  • 相关阅读:
    laravel 文件上传总结
    报错:SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry 'admin' for key 'username'...
    php下intval()和(int)转换使用与区别
    js向input的value赋值
    北京ArcGis Server开发培训例子(整理)
    C#中Tostring参数机用法详解(转)
    ArcGIS Server 10 服务器要求
    arcgis10 arcmap mxd文档所有保存为相对路径
    google map api简单例子1 定位预览
    ASTER GDEM简介 30m,dem免费下载
  • 原文地址:https://www.cnblogs.com/conquer/p/1388716.html
Copyright © 2011-2022 走看看