1

如何在 DataGridView 的 CheckBoxColumn 中订阅 CheckBox 的事件处理程序,类似于常规 CheckBox 的 CheckChanged 或 Click 事件处理程序?我的应用程序的多个数据网格中有一个或多个列,我想为此执行此操作。

我查看了 CellContentClick 和 CellClick,但它们似乎完全不优雅,因为它们会在数据网格中的每次单击时触发,而不仅仅是针对感兴趣的 Cell 或 CheckBox。

4

2 回答 2

3

下面的解决方案来自于 MSDN 文档,以及在这里和 CodeProject 上的一些切线线程中的一点运气。

这个想法是创建一个派生自 DataGridViewCheckBoxCell 的类,其中包括一个与 ContentClick 一起触发的处理程序。这对于一个 DataGridView 来说可能看起来有很多开销,但我的应用程序有很多 DataGridView,所以这段代码是可重用的,从而节省了时间。

/// <summary>
/// DataGridView cell class for check box cells with a OnContentClick event handler.
/// </summary>
public class DataGridViewEventCheckBoxCell : DataGridViewCheckBoxCell
{
    /// <summary>
    /// Event handler for OnContentClick event.
    /// </summary>
    protected EventHandler<DataGridViewCellEventArgs> ContentClickEventHandler { get; set; }

    /// <summary>
    /// Empty constructor. Required. Used by Clone mechanism
    /// </summary>
    public DataGridViewEventCheckBoxCell()
        : base()
    { }

    /// <summary>
    /// Pass through constructor for threeState parameter.
    /// </summary>
    /// <param name="threeState">True for three state check boxes, True, False, Indeterminate.</param>
    public DataGridViewEventCheckBoxCell(bool threeState)
        : base(threeState)
    { }

    /// <summary>
    /// Constructor to set the OnContentClick event handler.  
    /// Signature for handler should be (object sender, DataGridViewCellEventArgs e)
    /// The sender will be the DataGridViewCell that is clicked.
    /// </summary>
    /// <param name="handler">Handler for OnContentClick event</param>
    /// <param name="threeState">True for three state check boxes, True, False, Indeterminate.</param>
    public DataGridViewEventCheckBoxCell(EventHandler<DataGridViewCellEventArgs> handler, bool threeState)
        : base(threeState)
    {
        ContentClickEventHandler = handler;
    }

    /// <summary>
    /// Clone method override.  Required so CheckEventHandler property is cloned.
    /// Individual DataGridViewCells are cloned from the DataGridViewColumn.CellTemplate
    /// </summary>
    /// <returns></returns>
    public override object Clone()
    {
        DataGridViewEventCheckBoxCell clone = (DataGridViewEventCheckBoxCell)base.Clone();
        clone.ContentClickEventHandler = ContentClickEventHandler;
        return clone;
    }

    /// <summary>
    /// Override implementing OnContentClick event propagation 
    /// </summary>
    /// <param name="e">Event arg object, which contains row and column indexes.</param>
    protected override void OnContentClick(DataGridViewCellEventArgs e)
    {
        base.OnContentClick(e);
        if (ContentClickEventHandler != null)
            ContentClickEventHandler(this, e);
    }

    /// <summary>
    /// Override implementing OnContentDoubleClick event propagation 
    /// Required so fast clicks are handled properly.
    /// </summary>
    /// <param name="e">Event arg object, which contains row and column indexes.</param>
    protected override void OnContentDoubleClick(DataGridViewCellEventArgs e)
    {
        base.OnContentDoubleClick(e);
        if (ContentClickEventHandler != null)
            ContentClickEventHandler(this, e);
    }
}

因为我希望能够从列类中引用这个单元格类,所以我还实现了一个派生自 DataGridViewCheckBoxColumn 的类:

/// <summary>
/// DataGridView column class for a check box column with cells that have an OnContentClick handler.
/// </summary>
public class DataGridViewEventCheckBoxColumn : DataGridViewCheckBoxColumn
{
    /// <summary>
    /// Empty constructor.  Pass through to base constructor
    /// </summary>
    public DataGridViewEventCheckBoxColumn()
        : base()
    { }

    /// <summary>
    /// Pass through to base constructor with threeState parameter
    /// </summary>
    /// <param name="threeState">True for three state check boxes, True, False, Indeterminate.</param>
    public DataGridViewEventCheckBoxColumn(bool threeState)
        : base(threeState)
    { }

    /// <summary>
    /// Constructor for setting the OnContentClick event handler for the cell template.
    /// Note that the handler will be called for all clicks, even if the DataGridView is ReadOnly.
    /// For the "new" state of the checkbox, use the EditedFormattedValue property of the cell.
    /// </summary>
    /// <param name="handler">Event handler for OnContentClick.</param>
    /// <param name="threeState">True for three state check boxes, True, False, Indeterminate.</param>
    public DataGridViewEventCheckBoxColumn(EventHandler<DataGridViewCellEventArgs> handler, bool threeState)
        : base(threeState)
    {
        CellTemplate = new DataGridViewEventCheckBoxCell(handler, threeState);
    }
}

修剪掉多余的代码,我像这样使用它:

public void AddCheckBoxColumn(DataGridView grid, EventHandler<DataGridViewCellEventArgs> handler, bool threeState)
{
   grid.Columns.Add(new DataGridViewEventCheckBoxColumn(handler, threeState));
}

列类可以去掉,可以按如下方式使用:

public void AddCheckBoxColumn(DataGridView grid, EventHandler<DataGridViewCellEventArgs> handler, bool threeState)
{
   DataGridViewCheckBoxColumn column = new DataGridViewCheckBoxColumn(threeState);
   column.CellTemplate = new DataGridViewEventCheckBoxCell(handler, threeState);
   grid.Columns.Add(column);
}

这是一个示例事件处理程序。数据网格中另一列的状态会根据该列的值和 WasReleased 属性中的某些状态信息进行更新。DataGridView 的 DataSource 是 Specimen 对象的集合,所以每一行的 DataBoundItem 都是一个 Specimen。Specimen 类是特定于应用程序的,但它具有属性 OnHold,由该列显示;IsReleased,由另一列显示;和被释放。

    public static void OnHoldCheckClick(object sender, DataGridViewCellEventArgs e)
    {
        if (sender is DataGridViewEventCheckBoxCell)
        {
            DataGridViewEventCheckBoxCell cell = sender as DataGridViewEventCheckBoxCell;

            if (!cell.ReadOnly)
            {
                // The rows in the DataGridView are bound to Specimen objects
                Specimen specimen = (Specimen)cell.OwningRow.DataBoundItem;
                // Modify the underlying data source
                if ((bool)cell.EditedFormattedValue)
                    specimen.IsReleased = false;
                else if (specimen.WasReleased)
                    specimen.IsReleased = true;
                // Then invalidate the cell in the other column to force it to redraw
                DataGridViewCell releasedCell = cell.OwningRow.Cells["IsReleased"];
                cell.DataGridView.InvalidateCell(releasedCell);
            }
        }
    }

好的样式可能会将事件处理程序中的一些代码下推到 Specimen 类中的方法中。

于 2012-11-14T19:39:01.353 回答
1

您可以使用

e.Item.Cells[1].Text

这里 e 是当前行和第二个单元格记住单元格索引总是从 0 索引开始

于 2012-11-13T01:32:42.777 回答