I have a base class that extends DataGridView:
public abstract class MyDataGridView : DataGridView
I then have a class that extends MyDataGridView. Here is the class definition and the constructor:
// ::FunctionGridView
public class FunctionGridView : MyDataGridView
{
private static int NUM_COLUMNS = 12;
private static int NUM_ROWS = 1;
public FunctionGridView()
{
// call the method in the base class to setup the grid
setupGridView(NUM_ROWS, NUM_COLUMNS);
}
}
The column type used for my data grid is DataGridViewCheckBoxColumn. Here is the code to setup the columns (defined in the base class):
// ::MyDataGridView
protected void setupGridView(int numRows, int numColumns)
{
for (int i = 0; i < numColumns; i++)
{
DataGridViewCheckBoxColumn column = new DataGridViewCheckBoxColumn();
column.Name = "Column" + (i + 1).ToString();
this.Columns.Add(column);
}
}
I want to catch the SelectionChanged event when the user clicks a cell and then toggle the checkbox for the selected cell. The callback is set in the base class constructor as follows:
// ::MyDataGridView
// initialize the controls then add the event handler
public MyDataGridView()
{
initialize();
this.SelectionChanged += selectionChanged;
}
The selection changed callback is defined as follows:
// ::MyDataGridView
// get the checkbox _checked_ value and toggle it for the selected cell
private void handleSelectionChanged(object sender, EventArgs e)
{
int currentRow = this.CurrentCell.RowIndex;
int currentColumn = this.CurrentCell.ColumnIndex;
bool cellChecked = (bool)this.Rows[currentRow].Cells[currentColumn].Value;
this.Rows[currentRow].Cells[currentColumn].Value = !cellChecked;
}
Everything works fine except there is one anomaly that I don’t know how to handle..
When the program starts and the DataGridView is constructed and added to my Panel the SelectionChanged event is thrown initially for Row[0].Cell[0]. This event is being called even though the user never actually clicked on that cell.
The result is, when my program starts, the first cell gets checked without any user click. My data grid view ends up looking like the following:

Do I need a way to determine if the SelectionChanged callback is being thrown during initialization and not from a user click then exit my callback without toggling the checkbox value?
// ::MyDataGridView
private void selectionChanged(object sender, EventArgs e)
{
**// if (e.isInitializing()) return;**
....
....
}
I seem to recall in the Java ActionEvent class you can call getSource to make sure the event was triggered by the control and not some other source. Is there something equivalent in C#?
==============================================================================
NOTE to keep my question simple I used basic names and left out most of the detail. The real class/base class names are FunctionKeyTimer and KeyTimer (base class). If it helps, here is the class diagram and the full source code:

public abstract class KeyTimer : DataGridView
{
public static int WIDTH = 640;
public static int HEIGHT = 40;
private bool initialCheckedValue = false;
public KeyTimer()
{
initialize();
this.SelectionChanged += selectionChanged;
}
public KeyTimer(bool initialChecked)
{
this.initialCheckedValue = initialChecked;
initialize();
this.SelectionChanged += selectionChanged;
}
protected void setupGridView(int numRows, int numColumns, string columnNamePrefix)
{
for (int i = 0; i < numColumns; i++)
{
DataGridViewCheckBoxColumn column = new DataGridViewCheckBoxColumn();
column.Resizable = DataGridViewTriState.False;
column.SortMode = DataGridViewColumnSortMode.NotSortable;
column.Name = columnNamePrefix + (i + 1).ToString();
this.Columns.Add(column);
}
for (int i = 0; i < numRows; i++)
{
DataGridViewRow row = new DataGridViewRow();
this.Rows.Add(row);
}
foreach (DataGridViewRow row in this.Rows)
{
for (int i = 0; i < numColumns; i++)
{
row.Cells[i].Value = this.initialCheckedValue;
}
}
}
private void selectionChanged(object sender, EventArgs e)
{
int currentRow = this.CurrentCell.RowIndex;
int currentColumn = this.CurrentCell.ColumnIndex;
bool cellChecked = (bool)this.Rows[currentRow].Cells[currentColumn].Value;
this.Rows[currentRow].Cells[currentColumn].Value = !cellChecked;
}
private void initialize()
{
this.AllowUserToDeleteRows = false;
this.AllowUserToResizeColumns = false;
....
....
}
}
public class FunctionKeyTimer : KeyTimer
{
private static int NUM_COLUMNS = 12;
private static int NUM_ROWS = 1;
private static string COLUMN_NAME_PREFIX = "F";
public FunctionKeyTimer()
{
setupGridView(NUM_ROWS, NUM_COLUMNS, COLUMN_NAME_PREFIX);
}
}
Ok, well I never had an issue with events like that when I used the form editor. The events wouldn’t be called until the components were drawn.
I think it is because you are adding your event handler, before you programmaticly check the box. Hence it is getting triggered by your own code. Put some debug messages in to determine the order of operation. It might be as simple as moving some functions around.
Other than that, try printing out sender.name in your event listener to see if it changes between a user check and a programmatic check.
I hope that helps 🙂
~ Dan