How to have a DataGridViewComboBoxCell ?

Paraman

Member
Joined
Oct 19, 2024
Messages
14
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 !
 
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.
 
Back
Top Bottom