views:

23

answers:

1

I have a DataGridView where each row has a checkbox in it. I only want the selected row to change when the user clicks on one of the text cells. However, if a checkbox is clicked I would still like to have the checkbox change its state and catch this event.

I have searched everywhere and found a couple of solutions that fix half of the problem, but I haven't found anything that 100% prevents the selected row from changing when I click on my checkboxes.

A: 

Figured out a work around.

Instead of listening for SelectionChanged events, I listened for CellMouseClick events and then toggled my own flag for which row is selected.

I also changed the default row style so that there is no indication of which row is selected. Then i added some code to change the row style of whichever row was selected according to my own row.


Below is the code just for listening to CellMouseClicks on certain columns, the rest is very specific to my application.

    void dataGridView_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
    {
        // Make sure it is a left click
        if(e.Button == MouseButtons.Left)
        {
            // Make sure it is on a cell
            if (e.ColumnIndex >= 0 && e.RowIndex >= 0)
            {
                // Only allow certain columns to trigger selection changes (1 & 2)
                if (e.ColumnIndex == 1 || e.ColumnIndex == 2)
                {
                    // Set my own private selected row index
                    setSelectedRow(e.RowIndex);
                }
                else
                {
                    // Actions for other columns...
                }
            }
        }
    }
tbridge