Home > Blockchain >  DataGridView detect cell value change (checkbox)
DataGridView detect cell value change (checkbox)

Time:12-09

I have a DataGridView with a list<> (global variable) as datasource, the list is a list of a self made user data type. The first value in the UDT is a boolean so it shows a checkbox in the DataGridView.

When I check/uncheck a checkbox the boolean value choud be changed in the list<>.

It works with the following code, but it loops through every row and checks the checkbox

private void dgvObjecten_CellContentClick(object sender, DataGridViewCellEventArgs e)
    {
        for (int i = 0; i < dgvObjecten.Rows.Count; i  )
        {
            Dig1Objecten[i].Import = (bool)dgvObjecten.Rows[i].Cells["Import"].Value;
        }
        
        //refresh DataGridView
        dgvObjecten.DataSource = Dig1Objecten;

    }

Is there a way to get the row number of the checkbox that gets changed so it doesn't have to loop through all rows every time 1 checkbox is changed?

CodePudding user response:

In WinForms the typical event handler will have a sender object (the Control, Form, or other that sent the event) and an EventArgs structure of various types that provide the contextual information for the event. In this case, the RowIndex and the ColumnIndex can be determined by inspecting the e argument (easy as

  • Related