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();
        }
    }
    
  • 相关阅读:
    c99柔性数组
    Android自定义XML属性以及遇到的命名空间的问题
    [翻译]API Guides
    使用线程实现视图平滑滚动
    [翻译]API Guides
    [翻译]API Guides
    [翻译]Android官方文档
    探究Android中通过继承ViewGroup自定义控件的原理
    初探Android动画之门
    ViewPager、Fragment、Matrix综合使用实现Tab滑页效果
  • 原文地址:https://www.cnblogs.com/dlbird/p/3812286.html
Copyright © 2011-2022 走看看