"calling SetCurrentCellAddressCore" in event handlers - only where the row and column indices of the cell are equal

I am making a WinForms application that includes a form that uses DataGridViewto handle simple data operations. To ensure accurate recording when mitigating interference (read: without use DataGridViewComboBoxColumn), I have several event handlers that temporarily turn DataGridViewTextBoxCellinto an equivalent DataGridViewComboBoxCell, connected to values ​​that are known to be β€œclean” when events are edited, usually when you click on an edited cell ):

private void OnCellEndEdit(object sender, DataGridViewCellEventArgs e)
{
    //construct a textbox cell and set to current value
    DataGridViewTextBoxCell cell = new DataGridViewTextBoxCell();
    cell.Value = dataGridView.Rows[e.RowIndex].Cells[e.ColumnIndex].Value;

    //color row if invalid or modified
    UpdateRowStyle(dataGridView.Rows[e.RowIndex]);

    //switch to the new cell and redraw
    dataGridView.Rows[e.RowIndex].Cells[e.ColumnIndex] = cell;
    dataGridView.Refresh();
}

and

private void OnCellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
{
    //construct a combobox cell, link to data, and set to current value
    DataGridViewComboBoxCell cell = new DataGridViewComboBoxCell();
    cell.DataSource = mDropDownValues[dataGridView.Columns[e.ColumnIndex].Name];
    cell.Value = dataGridView.Rows[e.RowIndex].Cells[e.ColumnIndex].Value;

    //switch to the new cell and redraw
    dataGridView.Rows[e.RowIndex].Cells[e.ColumnIndex] = cell;
    dataGridView.Refresh();
}

- DataGridViewTextBoxCell, , , , DataGridViewComboBoxCell, , mDropDownValues[]. , , . , dataGridView.Rows[e.RowIndex].Cells[e.ColumnIndex] = cell; ( CellBeginEdit, CellEndEdit).

" , SetCurrentCellAddressCore."

, DataGridView? - , ?

+4
1

SetCurrentCellAddressCore(), :

        // Allow the code to be re-entrant only as a result of
        // underlying data changing.
        if (this.dataGridViewOper[DATAGRIDVIEWOPER_inCurrentCellChange] && 
           (this.dataConnection == null || !this.dataConnection.ProcessingListChangedEvent))
        {
            throw new InvalidOperationException(SR.GetString(SR.DataGridView_SetCurrentCellAddressCoreNotReentrant));
        }

DATAGRIDVIEWOPER_inCurrentCellChange true. DataGridView , , . , , , , .

- , , , . BeginInvoke(), , . :

private void OnCellEndEdit(object sender, DataGridViewCellEventArgs e)
{
    this.BeginInvoke(new Action(() => {
        //construct a textbox cell and set to current value
        DataGridViewTextBoxCell cell = new DataGridViewTextBoxCell();
        cell.Value = dataGridView.Rows[e.RowIndex].Cells[e.ColumnIndex].Value;

        //color row if invalid or modified
        UpdateRowStyle(dataGridView.Rows[e.RowIndex]);

        //switch to the new cell and redraw
        dataGridView.Rows[e.RowIndex].Cells[e.ColumnIndex] = cell;
        dataGridView.Refresh();
    }));
}
+5

Source: https://habr.com/ru/post/1568804/


All Articles