Search code examples
.netvb.netdatagridviewcomboboxcell

How can I prevent users from entering data into DataGridViewComboBoxCells?


I have a project that uses a number of DataGridView controls. Most of the cells are of the DataGridViewTextBoxCell persuasion. I declare my controls like so:

Dim MyCell as DataGridViewCell

Later I specify whether they are DataGridViewTextBoxControls or DataGridViewComboBoxCells like so:

MyCell = New DataGridViewTextBoxCell   ...or...
MyCell = New DataGridViewComboBoxCell

None of the places in my code require the ability for the users to enter their own values in the Combo Boxes. That is, they are either hard-coded or the values are gathered from other data within the application.

Currently, users can highlight a combo box control defined as shown above and begin typing anything they like. For example, I have one combo box that offers the user a selection of integers between 1 and 9. I can highlight the combo box cell and enter "Hello, World!" if I'd like.

What does it take to disable this ability? I'm sure there's a property, but I have yet to find it. I have searched the Internet, and have found only the ability to have user-entered values added to the list and a stream of data-binding tutorials.

Thank you in advance! C


Solution

  • Check here

    EDIT

    I probably should have put more information up, but I'm tired and feeling a little sick so I felt like being lazy ... but no you have to ruin my laziness and force me to type some more. (LOL just joking).

    Essentially the comboxbox that you see in a comboboxcell is acutally a combobox control (Tired = Bad Engrish).

    From what I can recall, you should be able to get at the control used in the cell by using the EditingControlShowing event of the datagridview. Once there you just set the dropdownstyle.

    Like:

    Private Sub datagridview_EditingControlShowing(ByVal sender As Object, ByVal e As DataGridViewEditingControlShowingEventArgs)
    
        Dim c As ComboBox = e.Control
    
        If Not c Is Nothing Then
    
            c.DropDownStyle = ComboBoxStyle.DropDownList
    
        End If
    
    End Sub