Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8834053
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T08:55:33+00:00 2026-06-14T08:55:33+00:00

How can I subscribe to an event handler of a CheckBox in a CheckBoxColumn

  • 0

How can I subscribe to an event handler of a CheckBox in a CheckBoxColumn of a DataGridView similar to the CheckChanged or Click event handler of a regular CheckBox? I have one or more columns, in multiple data grids in my application that I want to do this for.

I’ve looked at CellContentClick and CellClick, but they seem totally inelegant, because they fire for every click in the data grid, not just for the Cell or CheckBox of interest.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T08:55:34+00:00Added an answer on June 14, 2026 at 8:55 am

    The solution below came from pouring over the MSDN documentation and a little luck in some tangential threads here and on CodeProject.

    The idea is to create a class, derived from DataGridViewCheckBoxCell, that includes a handler that is triggered along with the ContentClick. This may seem like lots of overhead for one DataGridView, but my application has many DataGridViews, so this code is reusable, thus saving time.

    /// <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);
        }
    }
    

    Since I wanted to be able to reference this cell class from a column class, I also implemented a class derived from 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);
        }
    }
    

    Trimming away extraneous code, I’m using it like this:

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

    The column class could be eliminated, and it could be used as follows:

    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);
    }
    

    Here’s a sample event handler. The state of another column in the data grid is updated, based on the value of this column and conditionally some state information in the WasReleased property. The DataGridView’s DataSource is a collection of Specimen objects, so each row’s DataBoundItem is a Specimen. The Specimen class is application specific, but it has properties OnHold, displayed by this column; IsReleased, displayed by another column; and WasReleased.

        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);
                }
            }
        }
    

    Good style would probably push some of the code in the event handler down into a method in the Specimen class.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In my class I want to declare an event that other classes can subscribe
Hai every one, I am developing an application where users can subscribe or unsubscribe
Similar to the ability to subscribe to a user's posts on Facebook, users can
How can a field value of a Component be overridden using Event Handler? When
I try to subscribe a event handler to the data received event. Seems like
I have an image with a click event bound to it which grows/shrinks the
If you subscribe the .net event with the same subscribe more then once, then
I've built a WCF Publish Subscribe Topic Service and can successfully publish/subscribe with a
I have a form where a subscriber can select the newsletters they want to
I have some items called tickers which can be created and subscribed to. When

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.