简体   繁体   中英

DataGridView detect cell value change (checkbox)

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?

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 ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM