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 8408773
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T23:43:25+00:00 2026-06-09T23:43:25+00:00

I have a base class that extends DataGridView : public abstract class MyDataGridView :

  • 0

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:

DataGridView Initial Cell Checked

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:

Class Diagram

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);
    }
}
  • 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-09T23:43:26+00:00Added an answer on June 9, 2026 at 11:43 pm

    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

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

Sidebar

Related Questions

If I have a base class such that public abstract class XMLSubscription <T extends
I have an abstract base class that holds a Dictionary. I'd like inherited classes
I have a abstract base class that I have many inherited classes coming off
So I have a base class with a constructor that takes (T V) .
I have a class that extends a base class. The base class is more
I have a base class and a a second class that's extends the base
If I have a base class that contains a static var, I then set
I have an abstract base class that I use for services which includes the
I have custom class that extends WebViewPage that I use as the base for
I have a base class that has an abstract getType() method. I want subclasses

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.