Pregunta

I have a DGV bound to an IEnumerable. T has a boolean property which can be get/set. When loading the table, the get property is accessed to populate the DataGridViewCheckboxColumn. I can see it being hit in the debugger. When I click a cell in the checkbox column, the checkbox is checked as you'd expect, but the underlying data source isnt' updated, and the property setter isn't called. The checkbox column has the ReadOnly property set to false.

How can I get this to update the underlying bound data?

polygonListBindingSource.DataSource = m_displayPolygons.OrderBy(p => p.Name);

I've seen related questions, but they're not consistant in their answers. Some suggest calling EndEdit on the DGV, other suggest calling it on the binding source. Do I really have to subscribe to an event for when the cell value is changed to actually commit the change to the underlying data type? This seems unnatural.

¿Fue útil?

Solución

This is down to the fact that the DataGridView only commits its edits when the current cell loses focus. Clicking on the checkbox isn't enough to commit the edit.

The standard was to do this is to use events, usually the CurrentCellDirtyStateChanged event:

void dataGridView1_CurrentCellDirtyStateChanged(object sender, EventArgs e)
{
    if (dataGridView1.IsCurrentCellDirty)
    {
        dataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit);
    }
}

It is a bit unnatural but is by design - this way your data source doesn't get flooded by edits that may not be final. Not necessarily such a big deal for a checkbox but a worthwhile design decision for the text box cell.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top