We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
close icon

GridControl ComboBox only allow items in ChoiceList

I have a ComboBox in a GridControl. I set the items for the dropdown by assigning a ChoiceList.

How can I restrict the entries in that particular cell so that the only allowed values are the ones in the ChoiceList and the user cannot enter text from the keyboard? 

So basically I'm looking for ComboBoxStyle.DropDownList functionality but for a ComboBox in a GridControl cell: https://msdn.microsoft.com/en-us/library/system.windows.forms.comboboxstyle.aspx

6 Replies

TT tttmack October 8, 2015 04:53 PM UTC

I figured it out:
gridControl1[row, col].DropDownStyle = GridDropDownStyle.Exclusive;


SA Solai A L Syncfusion Team October 9, 2015 12:31 PM UTC

Hi Taylour,

Thank you for your interest in Syncfusion products.

You can restrict the editing of combo box cell by using the following code. Please refer the below sample and code for reference.

Code :

gridControl1[2, 3].CellType = "ComboBox";

            //Set datasource for the combobox  cell

gridControl1[2, 3].DataSource = items;

gridControl1[2, 3].CellValue = "Five";

gridControl1[2, 3].ExclusiveChoiceList = true;

gridControl1[2, 3].ReadOnly = true;

Sample :
http://www.syncfusion.com/downloads/support/forum/120719/ze/ComboBoxCell-CS-1433202679

Thanks & Regards,
AL.Solai.



TT tttmack October 9, 2015 03:34 PM UTC

Hi AL.Solai., thanks for your help.

There is a problem with your sample code in that by adding ReadOnly = true, you can't change the value of the ComboBox using the mouse (note that your second example in cell 2,4 also doesn't show up because you have ReadOnly set to true before setting the cell value). If we don't set ReadOnly to true, then the user can highlight the cell and press the delete key to delete the cell value.
Is there any way around this?

Also the documentation (in Intellisense) for ExclusiveChoiceList says to use DropDownStyle instead. I assume DropDownStyle = GridDropDownStyle.Exclusive is equivalent?




SA Solai A L Syncfusion Team October 12, 2015 12:23 PM UTC

Hi Taylour,

Thank you for your update.

To restrict adding data from the Combo box, use the following two properties.
Code :


// User input is restricted to datasource/choicelist. 

gridControl1[2, 3].ExclusiveChoiceList = true;

// User input is Restricted to items from datasource/choicelist.

gridControl1[2, 3].DropDownStyle = GridDropDownStyle.Exclusive;


Thanks,
AL.Solai.



TT tttmack October 12, 2015 04:53 PM UTC


Hi, the two lines of code you provided were not enough as they do not handle the case when the user presses the delete or backspace key which allows empty values in the cell. I added these two events and suppressed the keys to get the effect I wanted to achieve.

        void gridControl1_CurrentCellKeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Delete || e.KeyCode == Keys.Back)
                e.Handled = true;
        }

        void gridControl1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Delete)
                e.Handled = true;
        }


SA Solai A L Syncfusion Team October 13, 2015 11:29 AM UTC

Hi Taylour,

Thank you for your update.

The given properties will restrict the user from adding the input to combo box choice list / data source. The data given by the user will be temporarily viewed (it will not be updated in the combo box). Finally, we are glad that the requirement has been achieved.


Thanks & Regards, 

AL.Solai.


Loader.
Live Chat Icon For mobile
Up arrow icon