views:

8876

answers:

6

Hi

I have a grid view that has a check box cloumn, and I want to trigger a drawing even as soon as the value of the cell is toggled. I tried the ValueChaged and the CellEndEdit and BeginEdit, and chose the selection mode as CellSelect. As for the The first 2 events, the event was triggered upon the finishing of the edit mode, like moving out of the current cell, or going back and forth. It's just a weird behavior.

Is there anything that triggers the event on the grid view as soon as the cell value is changed?

Best Regards,

+1  A: 

Try hooking into the CellContentClick event. The DataGridViewCellEventArgs will have a ColumnIndex and a RowIndex so you can know if a ChecboxCell was in fact clicked. The good thing about this event is that it will only fire if the actual checkbox itself was clicked. If you click on the white area of the cell around the checkbox, it won't fire. This way, you're pretty much guaranteed that the checkbox value was changed when this event fires. You can then call Invalidate() to trigger your drawing event, as well as a call to EndEdit() to trigger the end of the row's editing if you need that.

BFree
but still the cell has to be deselected for this event to be triggered. So it you stay in the same cell and you click it for more than one time, the event wont be triggered
Mustafa A. Jabbar
A: 

I finally implemented it this way

  private void dataGridView1_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
    {

        if (e.ColumnIndex >= 0 && e.RowIndex >= 0)
        {
            if (dataGridView1[e.ColumnIndex, e.RowIndex].GetContentBounds(e.RowIndex).Contains(e.Location))
            {
                cellEndEditTimer.Start();
            }
        }

    }

    private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
    { /*place your code here*/}


    private void cellEndEditTimer_Tick(object sender, EventArgs e)
    {
        dataGridView1.EndEdit();
        cellEndEditTimer.Stop();
    }
Mustafa A. Jabbar
+1  A: 

A colleague of mine recommends trapping the CurrentCellDirtyStateChanged event. See http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.currentcelldirtystatechanged.aspx.

+1  A: 

I had the same issue, but came up with a different solution:

If you make the column or the whole grid "Read Only" so that when the user clicks the checkbox it doesn't change value.

Fortunately, the DataGridView.CellClick event is still fired. In my case I do the following in the cellclick event: if (jM_jobTasksDataGridView.Columns[e.ColumnIndex].CellType.Name == "DataGridViewCheckBoxCell") but you could check the column name if you have more than one checkbox column.

I then do all the modification / saving of the dataset myself.

James
A: 

cellEndEditTimer.Start();

this line makes the datagridview update the list of checked boxes

Thank you.

Leandro
A: 

Awesome Mustafa! I've been looking for this thing all over for the past 2 days.

rcrdo