in

Platinum Bay

Peace, Love, and...

This Blog

Syndication


.NETicated

Editable ComboBox

A colleague ran into an issue awhile back trying to edit data in a DataGrid. He had a ComboBox, and wanted to be able to edit the text to a value not bound to it.

The answer is really simple, but not well documented.

The first step is to create a class that derives from DataGridViewComboBoxCell. This is where the magic happens. By default, the DropDownStyle of the ComboBox in this column is set to ComboBoxStyle.DropDownList, which is not editable. We can change that by setting the DropDownStyle to ComboBoxStyle.DropDown.

Public Class EditableDataGridViewComboBoxCell
    Inherits DataGridViewComboBoxCell

    Public Overrides Sub InitializeEditingControl(ByVal rowIndex As Integer, ByVal initialFormattedValue As Object, ByVal dataGridViewCellStyle As DataGridViewCellStyle)
        MyBase.InitializeEditingControl(rowIndex, initialFormattedValue, dataGridViewCellStyle)

        Dim box1 As ComboBox = CType(MyBase.DataGridView.EditingControl, ComboBox)

        If (Not box1 Is Nothing) Then
            box1.DropDownStyle = ComboBoxStyle.DropDown
        End If
    End Sub
End Class

The next step is to create a control that derives from DataGridViewComboBoxColumn and sets the CellTemplate property to an instance of our EditableDataGridViewComboBoxCell

Public Class EditableDataGridViewComboBoxColumn
    Inherits DataGridViewComboBoxColumn

    Public Sub New()
        Me.CellTemplate = New EditableDataGridViewComboBoxCell
    End Sub
End Class

Finally in your form, set your column to be of type EditableDataGridViewComboBoxColumn.

Once you have this implementation in place, you can do things like override the change event, and save your new value to the underlying datasource.

Have fun!

Published Jun 06 2007, 02:26 AM by Steve
Filed under: ,

Comments

No Comments

Leave a Comment

(required )  
(optional )
(required )  
Add

About Steve

Steve Andrews is an independent consultant, INETA speaker, and Microsoft MVP for Visual Studio ALM. He has been working in technology for over ten years focusing on custom application development and Application Lifecycle Management. Steve is also Microsoft and IBM certified and a community fanatic having led sessions at nearly 100 events across North America. When he's not developing software solutions or engaging with the community about software technology, Steve is a closet singer and songwriter and plays the guitar and keys. Occasionally, Steve even gets to sleep. Occasionally.
Powered by Community Server (Commercial Edition), by Telligent Systems
© Platinum Bay | Some Rights Reserved Creative Commons License

Disclaimer: The information in this weblog is provided "AS IS" with no warranties, and confers no rights. This weblog does not represent the thoughts, intentions, plans or strategies of my employer. It is solely my opinion. Feel free to challenge me, disagree with me, or tell me I'm completely nuts in the comments section of each blog entry, but I reserve the right to delete any comment for any reason whatsoever (abusive, profane, rude, or annonymous comments) - so keep it polite, please.