zoukankan      html  css  js  c++  java
  • Winform中 DataGridView控件中的 CheckBox 的值读出来 始终 为 False ,已解决

            private void DGV_DetailsViewer_CellContentClick(object sender, DataGridViewCellEventArgs e)
            {
                if (e.ColumnIndex.Equals(5))
                {
                    DataGridViewCheckBoxCell cbx = (DataGridViewCheckBoxCell)this.DGV_DetailsViewer.Rows[e.RowIndex].Cells[e.ColumnIndex];
    
                    MessageBox.Show(cbx.FormattedValue.ToString());
                }
            }

    用的 以上这段代码 读出来 的checkBoxCell的值始终 为 False, 原因 不明。

    原因是 :CellContentClick 事件 并不能修改 CheckBoxCell的值,靠 这个事件 不行。

    正确的做法 应该是 利用 CurrentCellDirtyStateChanged 事件 和 CellValueChanged 事件 的 组合 来完成 这个功能。代码如下:

    //这个事件先执行,即使得 修改后的 checkboxcell的内容立刻生效,否则只有离开这个单元格时 才会生效。     
       private void DGV_DetailsViewer_CurrentCellDirtyStateChanged(object sender, EventArgs e)
            {
                if (DGV_DetailsViewer.IsCurrentCellDirty)
                {
                    DGV_DetailsViewer.CommitEdit(DataGridViewDataErrorContexts.Commit);
                }
            }
    
    //值修改后,就执行这个事件,想要实现的功能 在这个事件里实现
     private void DGV_DetailsViewer_CellValueChanged(object sender, DataGridViewCellEventArgs e)
            {
                if (e.RowIndex >= 0 && e.RowIndex != -1 && !this.DGV_DetailsViewer.Rows[e.RowIndex].IsNewRow)
                {
    
                    if (e.ColumnIndex.Equals(5))
                    {
                        DataGridViewCheckBoxCell cbx = (DataGridViewCheckBoxCell)this.DGV_DetailsViewer.Rows[e.RowIndex].Cells[e.ColumnIndex];
    
                        MessageBox.Show(cbx.FormattedValue.ToString());
                    }
                }
            }
  • 相关阅读:
    忆2011年的秋天:一个人的项目
    横看成岭侧成峰,远近高低各不同——从面试官的角度谈面试
    使用Scratch进行少儿编程
    初识少儿编程
    升级openssl
    CentOS设置虚拟网卡做NAT方式和Bridge方式桥接
    iptables conntrack有什么用
    nohup和&的区别
    Linux就这个范儿 第12章 一个网络一个世界
    一个由INode节点爆满引起的业务故障
  • 原文地址:https://www.cnblogs.com/Chendezhou/p/8981632.html
Copyright © 2011-2022 走看看