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.
 
Back
Top Bottom