Исключение при отправке данных из DataGridViewComboBox в TextBoxCell - PullRequest
0 голосов
/ 14 февраля 2019

До сих пор мне удавалось обработать событие, когда значение comboBox изменилось, но когда я хочу обновить содержимое другой ячейки этим значением, я получаю 'System.NullReferenceException'.

Это код, который я использую для обработки события (я нашел его в другом вопросе)

void dataGridView1_CurrentCellDirtyStateChanged(object sender, EventArgs e)
{
    if (this.dataGridBebidas.IsCurrentCellDirty)
    {
        // This fires the cell value changed handler below
        dataGridBebidas.CommitEdit(DataGridViewDataErrorContexts.Commit);
    }
}

private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
    // My combobox column is the second one so I hard coded a 1, flavor to taste
    DataGridViewComboBoxCell cb = (DataGridViewComboBoxCell)dataGridBebidas.Rows[e.RowIndex].Cells[4];
        if (cb.Value != null)
        {
            // Asigno valor de combobox a campo "Alcohol"
            dataGridBebidas.Invalidate();

            var currentcell = dataGridBebidas.CurrentCellAddress;
            var sendingCB = sender as DataGridViewComboBoxEditingControl;
            DataGridViewTextBoxCell cel = (DataGridViewTextBoxCell)dataGridBebidas.Rows[currentcell.Y].Cells[3]; // Cell I want to update

            cel.Value = sendingCB.EditingControlFormattedValue.ToString(); // Asign value of the combobox

        }
}

Чего мне не хватает?

1 Ответ

0 голосов
/ 14 февраля 2019

Надеюсь, это то, что вы хотите.

void dataGridView1_CurrentCellDirtyStateChanged(object sender, EventArgs e)
{
    if (this.dataGridBebidas.IsCurrentCellDirty)
    {
        // This fires the cell value changed handler below
        dataGridBebidas.CommitEdit(DataGridViewDataErrorContexts.Commit);
    }
}

private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
    // My combobox column is the second one so I hard coded a 1, flavor to taste
    DataGridViewComboBoxCell cb = (DataGridViewComboBoxCell)dataGridBebidas.Rows[e.RowIndex].Cells[4];
    if (cb.Value != null)
    {
        // Asigno valor de combobox a campo "Alcohol"
        dataGridBebidas.Invalidate();

        dataGridBebidas.Rows[e.RowIndex].Cells[3].Value = cb.Value; // Cell I want to update
    }
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...