How to convert a DataGridViewComboBoxCell to a DataGridViewTextBoxCell

I searched a lot for this error, about which many questions have already been asked, but this is not a solution to my problem. I get

The operation is invalid because it leads to a repeated call to the SetCurrentCellAddressCore function.

Scenario: I have datagridviewwith TextboxColumnI use CellBeginEditto convert it to ComboBoxColumn, and after that CellValidateI change ComboBoxColumn to TextboxColumn again. Codes work for everyone. but getting the indicated error in the exact line e.RowIndex = 2raises this exception, but other doses of the lines do not show the error. if I omitted this error and continue, then e.RowIndex = 2 the cell value will be empty, and the value of other rows will work.

Here is the code CellBeginEdit

 if (e.ColumnIndex == 2 && e.RowIndex >= 0)
  {
    try
     {
      string s = Convert.ToString(_dgvCoarseAggegateTest[e.ColumnIndex, e.RowIndex].Value);
      string s1 = Convert.ToString(_dgvCoarseAggegateTest[e.ColumnIndex, 0].Value);
      DataGridViewComboBoxCell c = new DataGridViewComboBoxCell();

      string _SizeName = _cGetParrent._mGetParentCellValue(ref _dgvCoarseAggegateTest, e.RowIndex, 1);                  
      _mFillSieveSizeGridCombo(_mGetMetalSizeID(_SizeName), ref c); // Here My Combo Will GetValues from SQL and it Returning Value
      _dgvCoarseAggegateTest[e.ColumnIndex, e.RowIndex] = c; // Heres the error When e.RowIndex == 2 and if e.RowIndex != 2 then no error
      _dgvCoarseAggegateTest[e.ColumnIndex, e.RowIndex].Value = s;
      _dgvCoarseAggegateTest[e.ColumnIndex, 0].Value = s1;
     }
     catch (Exception ex)
      {
        MessageBox.Show(ex.Message);
      }
    }

How to resolve this.

: , - , , , ,

testTable
1      A
2      B
3      C
4      D
5      E
6      F
7      G
8      H
9      I

1 1 9, _mFillSieveSizeGridCombo id sql server 2008 Combo.Item.Add(x).

+4
3

SetCurrentCellAddressCore() , , DataGridView. = true reset .

, BeginInvoke() , async.

EditOnEnter, BeginInvoke

private bool _suppressCellBeginEdit = false;
private void dgv_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
{
    var dgv = sender as DataGridView;
    if (_suppressCellBeginEdit)
        return;

    if (e.ColumnIndex == 2 && e.RowIndex >= 0)
    {
        string s = Convert.ToString(dgv[e.ColumnIndex, e.RowIndex].Value);
        string s1 = Convert.ToString(dgv[e.ColumnIndex, 0].Value);
        DataGridViewComboBoxCell c = new DataGridViewComboBoxCell();

        c.Items.Add(string.Format("x{0}:y{1} {2}", e.RowIndex, e.ColumnIndex, 0));
        c.Items.Add(string.Format("x{0}:y{1} {2}", e.RowIndex, e.ColumnIndex, 1));
        c.Items.Add(string.Format("x{0}:y{1} {2}", e.RowIndex, e.ColumnIndex, 2));

        // special handling
        if (e.RowIndex == e.ColumnIndex)
        {
            this.BeginInvoke(new Action(() =>
            {
                _suppressCellBeginEdit = true;
                this.Invoke(new Action(() => 
                    {
                        c.Value = s;
                        dgv[e.ColumnIndex, e.RowIndex] = c;
                        dgv[e.ColumnIndex, 0].Value = s1;
                    }));
                _suppressCellBeginEdit = false;
            }));
        }
        else
        {
            c.Value = s;
            dgv[e.ColumnIndex, e.RowIndex] = c;
            dgv[e.ColumnIndex, 0].Value = s1;
        }
    }
}
+2

- , , DataGridView , . . , . , . , @Eric, , .

, , . , , . . ColumnType DataGridViewComboBoxColumn. CellBeginEdit combobox. :

    private void _dgvCoarseAggegateTest_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e) {
        var dgv = (DataGridView)sender;
        if (e.ColumnIndex == 2) {
            var cell = (DataGridViewComboBoxCell)dgv.Rows[e.RowIndex].Cells[e.ColumnIndex];
            cell.Items.Clear();
            // Run your dbase query here to fill cell.Items
            //...
            // We'll just fake it here for demo purposes:
            cell.Items.Add(e.RowIndex.ToString());
            cell.Items.Add((e.RowIndex+1).ToString());
            cell.Items.Add((e.RowIndex+2).ToString());
        }
    }
+2

: CellBeginEdit , ColumnType DataGridViewComboBoxCell. cancel, , , :

void switchCellType(object sender, DataGridViewCellCancelEventArgs e)
{
    DataGridViewComboBoxCell c = new DataGridViewComboBoxCell();
    // prepare the cell:
    //..
    // fill the drop down items..
    c.Items.Add("1");  // use
    c.Items.Add("2");  // your 
    c.Items.Add("3");  // code here!
    DGV[e.ColumnIndex, e.RowIndex] = c;  // change the cell
    DGV_CellBeginEdit(sender, e);        // restart the edit with the original parms  
}


private void DGV_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
{

    DataGridViewCell cell = DGV[e.ColumnIndex, e.RowIndex];
    if (!(cell is DataGridViewComboBoxCell))
    {
        e.Cancel = true;
        switchCellType(sender, e);
        return;
    }
    //..

, , . , .

, , CellEndEdit !! , , , Tag . , CellEndEdit, .

+1

All Articles