How to have a DataGridViewComboBoxCell ?

Paraman

Member
Joined
Oct 19, 2024
Messages
15
Programming Experience
10+
Hi, I am using Windows Forms. I wish to put ComboBoxCell for particular cells only. Is it possible ?
I tried by the following way
C#:
Expand Collapse Copy
        private void MyDataGrid1_CellEnter(object Sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex == 4)
            {
                if (e.ColumIndex >= 2 && e.ColumnIndex <= 7)
                {
                    DataGridViewComboBoxCell ImSubHdCell = new DataGridViewComboBoxCell();
                    ImSubHdCell.FlatStyle = FlatStyle.Popup;
                    ImSubHdCell.Items.Clear();
                    ImSubHdCell.Items.Add("MyValue1");
                    ImSubHdCell.Items.Add("MyValue2");
                    ImSubHdCell.Items.Add("MyValue3");
                    myDataGrid1[e.ColumnIndex, 4] = ImSubHdCell;
                }
            }

        }

        private void MyDataGrid1_CellLeave(object Sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex == 4)
            {
                if (e.ColumIndex >= 2 && e.ColumnIndex <= 7)
                {
                    DataGridViewTextBoxCell ImTxtBxCell = new DataGridViewTextBoxCell();
                    myDataGrid1[e.ColumnIndex, 4] = ImTxtBxCell;
                }
            }
        }
Error :- InvalidOperationException was unhandled - "Operation is not valid because it results in a reentrant call to the SetCurrentCellAddressCore function"

Suggestions will be much helpful !
Thanks Again !
 
Solution
The problem is that you are doing this in the CellEnter event handler. By creating a new cell in that event handler, you cause the event to be raised again. That's what the error message about the reentrant call is.

You should just have combo box columns there in the first place. Changing the cell or even the editing control type in that situation is silly. If you don't want the cells to look like ComboBoxes when you're not editing them then you can do that in the column configuration.
I think what you are trying to do is customize the edit control. This documentation is probably close to what you need:

That is for entire columns, though. I'm not sure how you would go about limiting to some rows as well. In your shoes, I would go for the expedient approach of making some rows read-only.
 
The problem is that you are doing this in the CellEnter event handler. By creating a new cell in that event handler, you cause the event to be raised again. That's what the error message about the reentrant call is.

You should just have combo box columns there in the first place. Changing the cell or even the editing control type in that situation is silly. If you don't want the cells to look like ComboBoxes when you're not editing them then you can do that in the column configuration.
 
Solution
My Kind Thanks To SkyDriver & JmCilHinney !

C#:
Expand Collapse Copy
    private: System::Void MyDataGrid1_CellEnter(System::Object^ Sender, DataGridViewCellEventArgs^ e) {
        if (e->RowIndex == 4 && e->ColumnIndex == 4) {
            if (myDataGrid1->CurrentCell != MyCmbGradeCell2) {
                MyCnt = MyCnt + 1;
                //MessageBox::Show("Count Of Cell Enter =  " + Convert::ToString(MyCnt)->Trim());
                MyRow = e->RowIndex;
                MyCol = e->ColumnIndex;
                this->BeginInvoke(gcnew MethodInvoker(this, &Form_MeltingAndPouring::UpdateCurrentCell));
            }
        }
    }
    private: System::Void UpdateCurrentCell() {
        // Safe to modify the DataGridView here
        try {
            myDataGrid1->Rows[MyRow]->Cells[MyCol] = MyCmbGradeCell2;
        }
        catch (Exception^ ex) {

        }
    }
 
Looks like C++/CLI to me...

I'm surprised that worked just doing a post message back into the UI thread.

Glad it works for you.
 
Last edited:
Back
Top Bottom