Note that there are some explanatory texts on larger screens.

plurals
  1. POC# DataGridView (CheckBox) Cell Click Multiple Callbacks
    primarykey
    data
    text
    <p>[C#, Visual Studio 2008, Windows 7 64]</p> <p>I have a <em>DataGridView</em> in my class. This data grid view uses <em>DataGridViewCheckBoxColumn</em> so that each cell contains a checkbox. </p> <p>Here is a screenshot of one of the rows:</p> <p><img src="https://i144.photobucket.com/albums/r177/tackyjan/datagridview_checkbox.png" alt="DataGridView Checkboxes"></p> <p>I want to be able to detect if the user selects the cell (somewhere in the cell but not on top of the checkbox). I also want to detect i the user selects the checkbox. In order to do this my code must set callbacks for both events:</p> <pre><code>this.CellClick += cellClick; // callback when the user selects a cell this.CellContentClick += cellContentClick; // callback when the user selects a checkbox </code></pre> <p>Here are the callback methods:</p> <pre><code> private void cellContentClick(object sender, DataGridViewCellEventArgs e) { toggleCellCheck(e.RowIndex, e.ColumnIndex); } private void cellClick(object sender, DataGridViewCellEventArgs e) { toggleCellCheck(e.RowIndex, e.ColumnIndex); } private void toggleCellCheck(int row, int column) { bool isChecked = (bool)this[column, row].EditedFormattedValue; this.Rows[row].Cells[column].Value = !isChecked; } </code></pre> <p>(<em>NOTE:</em> As you can see the <em>toggleCellCheck</em> method gets the checkbox value and toggles it, checked->unchecked or unchecked->checked.)</p> <p>When the user clicks anywhere in a cell that is not the checkbox only one callback is fired, <em>cellClick</em>. The <em>toggleCellCheck</em> method is subsequently called and the checkbox state flips.</p> <p>This is the exact behavior I want.</p> <p>The problem I am having is, when the user clicks directly on a checkbox <strong>both</strong> events get fired in the following order: <em>cellClick</em> then <em>cellContentClick</em>.</p> <p>Both callbacks being executed results in the checkbox <em>checked</em> state being toggled after the first callback and then toggled again after the second callback. The net result of course is the checkbox <em>checked</em> status does not change.</p> <p>Is there some way I can configure the <em>DataGridView</em> class to stop both callbacks from being fired? Or, is there a way I can detect (inside the <em>cellContentClick</em> method) that this is the <em>second</em> callback, or the callback was generated by clicking a checkbox, and then just exit without calling <em>toggleCellCheck</em>?</p> <p>I was thinking something like the following:</p> <pre><code>private void cellContentClick(object sender, DataGridViewCellEventArgs e) { // if sender/sender child/etc. is of type checkbox then return because // _cellClick_ has already been called to change the checkbox checked property // something like the following: // // if (typeof(sender) == CheckBox) return; // else toggleCellCheck(e.RowIndex, e.ColumnIndex); } </code></pre> <p>Thanks!</p> <p>Jan</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload