zoukankan      html  css  js  c++  java
  • DataGridView中DataGridViewComboBoxColumn的一些相关应用(一)让其值改变时触发事件-转

    转自 
    https://maodaili.de/mao.php?u=a%2FMrbEvUE8PnCuc7FrhJi0Rqd3kmOBHPZUbcJ1c2hbJUK0RYWpAf4lhIOddItP%2BKI2z5PZEiVpY%3D&b=15

    DataGridView中DataGridViewComboBoxColumn的一些相关应用(一)让其值改变时触发事件

    分类: Form
     今天在csdn回一个帖子的时候看到一个DataGridView问题,需要触发DataGridViewComboBoxCell中的事件才能够解决.

    打开vs试了下没有找到能直接触发DataGridViewComboBoxCell中combobox的值改变的事件,郁闷了半天,仔细看MSDN上有解决示例,都怪自己没有仔细看:

    首先需要触发第一个事件:CurrentCellDirtyStateChanged

    并且在事件中调用DataGridView.CommitEdit 方法 [关于CommitEdit MSDN解释如下:将当前单元格中的更改提交到数据缓存,但不结束编辑模式。 ]

    这样我们关心的那个事件CellValueChanged就能够被顺利触发了

    调用下MSDN上面对这个解决方式所提供的源码仅供参考:)

    // This event handler manually raises the CellValueChanged event
    // by calling the CommitEdit method.
    void dataGridView1_CurrentCellDirtyStateChanged(object sender,
        EventArgs e)
    {
        if (dataGridView1.IsCurrentCellDirty)
        {
            dataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit);
        }
    }
    
    // If a check box cell is clicked, this event handler disables  
    // or enables the button in the same row as the clicked cell.
    public void dataGridView1_CellValueChanged(object sender,
        DataGridViewCellEventArgs e)
    {
        if (dataGridView1.Columns[e.ColumnIndex].Name == "CheckBoxes")
        {
            DataGridViewDisableButtonCell buttonCell =
                (DataGridViewDisableButtonCell)dataGridView1.
                Rows[e.RowIndex].Cells["Buttons"];
    
            DataGridViewCheckBoxCell checkCell =
                (DataGridViewCheckBoxCell)dataGridView1.
                Rows[e.RowIndex].Cells["CheckBoxes"];
            buttonCell.Enabled = !(Boolean)checkCell.Value;
    
            dataGridView1.Invalidate();
        }
    }
    
  • 相关阅读:
    底部菜单栏之Fragment的详细介绍和使用方法
    Warm up 2
    如何做好一位资深的web前端工程师
    使用 HTML5 canvas 绘制精美的图形
    计算元素距离浏览器左边的距离
    [JSOI2016]独特的树叶
    【SDOI2009】Elaxia的路线
    【SCOI2009】最长距离
    【SCOI2009】围豆豆
    【AHOI2005】穿越磁场
  • 原文地址:https://www.cnblogs.com/dlbird/p/3812286.html
Copyright © 2011-2022 走看看