DataGridView ComboBox EditingControlShowing events

Fuginator picture Fuginator · Jul 15, 2011 · Viewed 10.3k times · Source

I have a basic WinForms app with d DataGridView (named dgvHardware) control on it, bound to this control is this code:

    private void dgvHardware_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
    {
        if (e.Control is ComboBox)
        {
            ((ComboBox)e.Control).SelectionChangeCommitted -= new EventHandler(cboDgvHardware_SelectionChanged);
            ((ComboBox)e.Control).SelectionChangeCommitted += new EventHandler(cboDgvHardware_SelectionChanged);
        }
    }

    private void cboDgvHardware_SelectionChanged(object sender, EventArgs e)
    {
        // string result is the selected text of the combo box
        // int row is the row index that the selected combo box lives
        string result = ((ComboBox)sender).SelectedItem.ToString();
        // int row = ????
    }

Long story short, I need to be able to determine the row that the events are being fired on, in order to change other cells in the row based on the ComboBox selection. The result in the second function returns correct values, for what it's worth.

Let me know if this is unclear or if you need any additonal information.

Thanks,
Andrew

Answer

Jay Riggs picture Jay Riggs · Jul 15, 2011

Use the DataGridView.CurrentCell Property like this:

int row = dgvHardware.CurrentCell.RowIndex;